Wim Hellenthal
02/26/2010 2:36 AM
post48382
|
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
|
|
|