Patrik Lahti
|
Re: Shared file descriptor
|
Patrik Lahti
02/26/2010 9:23 AM
post48403
|
Re: Shared file descriptor
On 26/02/10 02:36 AM, Wim Hellenthal wrote:
> Dear readers,
>
> I have a process A that connects to a remote server using standard TCP/IP stuff like:
> sid = socket(AF_INET, SOCK_STREAM, 0);
> .....
> connect (sid, .....)
> ....
> send(sid, ....)
> ....
> recv(sid,...)
>
> It runs in a seperate thread and does a kind of ping to make sure the remote side is sane (and also send some status
info).
>
> I also have a local server process B that listens for a remote connection
> and gets a file descriptor when it accepts the connection by means of the accept routine. This fd is then used to send
and receive to the remote side. When I share this fd with process A, process A can also send to the remote side just
like process B. This file sharing mechanism is discussed in the article by Thomas Fletcher.
>
Can you send a link to the paper? Are you using standard UNIX file
descriptor-passing? (SCM_RIGHTS)
> Summarizing, process A has now two connections open to the remote side. This mechanism just described works the first
time when running. However, when the remote connection to process B is lost and re-established, re-sharing the new file
descriptor with process A, then process A will give me back an invalid shared file descriptor (-1). I haven't got a
clue why.
>
When you say re-establish and re-share, what do you mean exactly?
Are you in Process A first doing another accept() and passing the
returned fd to Process B again? (did you check for errors from accept()?)
> I did some experiments.
> 1) For process A, when I leave out the thread that connects to the remote side the file sharing mechanism keeps
working no matter how many times I disconnect and re-establish a connection with server process B
>
> For process A, when I create the socket mis-using the flag SO_REUSEADDR like this:
> sid = socket(AF_INET, SOCK_STREAM | SO_REUSEADDR , 0);
>
SO_REUSEADDR is a socket option. You should use it with setsockop(). The
above code will have undefined behavior.
This socket option and SO_REUSEPORT may be necessary in your scenario...
/P
|
|
|