Project Home
Project Home
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Shared file descriptor: (3 Items)
   
Shared file descriptor  
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.

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. 

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);
the file sharing mechanism also keeps working. Unfortunatly now I can not connect to the remote side any more with this 
socket. 

Is there anyone who has a clue whats going on here? I realize I've entered the dungeons of network programming -:)

Thanks Wim  





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
Re: Shared file descriptor  
A link to the article:
      http://www.qnx.com/developers/articles/article_913_1.html

>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()?)
Yes exactly! that is what I mean. The accept succeeds and I can communicate.

>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...

Yep I tried both but no success. In fact,  creating a socket is al that is needed to get the behaviour.