|
From: Camille T. <ca...@os...> - 2012-06-04 10:07:18
|
Hello Stephen, I am resurrecting an old discussion. To refresh your memory, the problem was the following: 1- using liblo, TCP client sends data to a TCP server. 2- client stops sending data. 3- the server is closed. 4- now we try to open the server again on the same port. 5- -> results in "error port in use" and the server can not be open anymore. The server can be opened again only if we wait enough (about 1 minute), or this scenario happens: 1- using liblo, TCP client sends data to a TCP server. 2- client stops sending data. 3- the server is closed. 3b- the client sends a message to the closed server -> result in an expected error. 4- now we try to open the server again on the same port. 5- -> the server is opened again. A friend gave me what looks like a good solution: Set the SO_REUSEADDR flag on the server socket. So far, this works well for me and fixes a behavior I considered as a bug since a long time. Here is some documentation about this: > I've come across : Beej's Guide to Network Programming > > http://beej.us/guide/bgnet/ > > which seems quite good. It's up to date, ie it covers IPv4 and IPv6, and emphasises writing code that accommodates either. > > Still working through it, but I came across a bit (at the bottom of page 19 in section 5.3) that may help re Osculator reporting a 'port in use' error after un-pausing. The author also mentions that the problem goes away after a minute or so, which matches what you said. More specifically: >> Sometimes, you might notice, you try to rerun a server and bind() fails, claiming "Address already in use." What does that mean? Well, a little bit of a socket that was connected is still hanging around in the kernel, and it's hogging the port. You can either wait for it to clear (a minute or so), or add code to your program allowing it to reuse the port, like this: >> >> int yes=1; >> //char yes='1'; // Solaris people use this >> >> // lose the pesky "Address already in use" error message >> if (setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { >> perror("setsockopt"); >> exit(1); >> } And the patch is attached... Best, Cam |