connecting sockets - a summary
Dave Horsfall
dave at pta.OZ
Tue Nov 4 10:03:56 AEST 1986
For anyone interested, here is a summary of the responses I
got, asking whether I can connect two file descriptors together.
Sorry for the lateness.
------------------------------------------------------------
>From: rcodi at yabbie.rmit.oz (Ian Donaldson)
Spawing cat would be hideous. Why not just have a loop do it:
{
char buf[4096];
register int cc;
while((cc = read(pipefd, buf, 4096)) > 0)
write(acceptfd, buf, cc);
}
If your process would be rather doing other things, then fork this
loop instead of running it directly.
Ian Donaldson
*** Yes - I would rather be doing other things, and forking this
*** loop is as hideous as spawning a 'cat'.
*** Dave H.
------------------------------------------------------------
>From: Stewart Levin <seismo!hanauma!stew at munnari.oz>
Don't know the answer, but pipes are implemented these days as sockets.
So dig through the socket documentation and see if you can connect
sockets.
*** I did. Nothing. Which doesn't mean it isn't possible.
*** Dave H.
------------------------------------------------------------
>From: Ken Lalonde <kwlalonde at math.waterloo.cdn@munnari.oz>
Here's a thought: you can pass open file descriptors between
processes using sendmsg() on an AF_UNIX/SOCK_DGRAM socket.
Strange but true.
*** Except that these are AF_INET/SOCK_STREAM sockets. Pity.
*** Dave H.
------------------------------------------------------------
>From: mcvax!diku!thorinn at munnari.oz (Lars Henrik Mathiesen)
Some time ago there were some articles in one of the sources groups from Rick
Ace at New York Institute of Technology (philabs!nyit!rick) with the neccesary
kernel mods to connect a socket with either a controlling pseudo-tty or a "real
tty". This connection included some rlogin protocol processing.
As you talk about accept(), I assume you're on 4.2+BSD. Then your pipes are
special case sockets, so it's not the same situation. But if you can get Rick's
articles, you may be able to hack it up yourself - it ought to be simpler, as
you won't have to convert from mbufs to clists.
I'm sorry to be so vague about where it appeared, but I haven't kept the
headers. Try checking the mod.sources index.
*** Except we don't have a source licence. Pity.
*** Dave H.
------------------------------------------------------------
>From: Adam Quantrill <miduet!adam at munnari.oz>
So you want:
ACCEPT
|
V
___|_____ _______
| |--->---| |
| P1 | | P2 |
|_______| |_______|
with the accept fd connected to the pipe going to p2. Well perhaps a better
solution would be to dup the accept fd onto stdin, fork p2, and then restore
stdin ( you could save stdin by dup'ing it to another descriptor and then
dup'ing it back again). Then you would get:
ACCEPT
|
|--->---|
_________ | _______
| | |->--| |
| P1 | | P2 |
|_______| |_______|
Hope this helps,
--
-Adam.
/* If at first it don't compile, kludge, kludge again.*/
*** Yes - that is pretty well what I wanted to do, but I
*** got lost amongst the dups and forks ... I found another way
*** of doing it anyway, which had nothing to do with dups at all!
*** Amazing what you can do with a proper server/client situation.
*** Dave H.
------------------------------------------------------------
>From: oddjob!matt at lbl-csam.arpa@munnari.oz (Matt Crawford)
You could instead hand off the newly accepted fd to another
process using a unix domain socket and sendmsg(). If you
don't like that, go cat yourself!
_____________________________________________________
Matt University crawford at anl-mcs.arpa
Crawford of Chicago ihnp4!oddjob!matt
Will this never-ending series of PLEASURABLE EVENTS never cease?
*** I'm not using unix domain sockets.
*** Dave H.
------------------------------------------------------------
>From: Jonathan Hue <seismo!caip!lll-lcc!leadsv!msunix!jon at munnari.oz>
I must not understand the question. Doesn't dup2 do what you want?
dup2(oldd, newd)
int oldd, newd;
In the second form of the call, the value of newd desired is
specified. If this descriptor is already in use, the
descriptor is first deallocated as if a close(2) call had
been done first.
"If we did it like everyone else, Jonathan Hue
what would distinguish us from Via Visuals Inc.
every other company in Silicon Valley?" sun!sunncal\
>!leadsv!msunix!jon
"A profit?" amdcad!cae780/
*** Thereby losing the file descriptor I wanted to keep. See a
*** previous reply along the lines you suggest.
*** Dave H.
------------------------------------------------------------
Well - ladies, gentlemen and anything I may have missed, that
covers it. The solution was simple - invert the server/client
relationship, and everything just fell into place.
Thanks to everyone who responded, and my apologies for being late.
--
Dave Horsfall VK2KFU ISD: +61 2 957-2655 TLX: AA73857
Sun Computer Australia STD: (02) 957-2655 FAX: +61 2 923-2570
77 Pacific Hwy ACS: dave at pta.oz
Nth Sydney NSW 2060 UUCP: seismo!munnari!pta.oz!dave
AUSTRALIA ARPA: dave%pta.oz at seismo.arpa
More information about the Comp.unix.wizards
mailing list