Closing a socket

Started by
4 comments, last by ANSI2000 21 years, 6 months ago
I wrote a Win32 client application in C/C++ using Winsock 2. The application connects to a server from a third party... Here is where the problem occurs... When I close the socket on my client application using the closesocket(socketHandle) function, the server does not realise the socket was closed and continues to listen on that socket. So when I try to reconnect to the same I.p and Port I canot since the server is still in listening mode. The tird party claims I have to send a "disconect" command before closing the socket. I am not aware of any disconect functions is Winsock. Is this "disconect" command some byte value I have to send to there socket before calling closesocket()?
Advertisement
Are you using UDP because UDP has no concept of open or closed connections because it is connectionless.
Martin Piper
Sorry it''s TCP/IP
There is another Winsock Win32 API function to send the other system a notice before closing a socket.

shutdown()

Kuphryn
Fistly, enable the server to reuse the socket address : before bind(), do :

int param = 1;
setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &param, sizeof(param));

Secondly, if one end of a socket closes, it''ll be marked readable at the other end, and recv() on it will return zero bytes read, letting you know what happened.

That''s in the case of a graceful termination, of course. Otherwise, you need to use time-outs.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
As for the server I can''t do anything about it, it''s third party...

I started using WSADisconect()... It seemed to work fine, I started and shut down my app about a dozen times then the error started again. I remeber using shutdown() and it did the same thing. It worked a dozen times, then it caused the same error and it made me give up on it So every time this hapens I have to call up the third party and ask them to reset their socket...

Maybe my app exits to quick before the FIN command makes it through... Maybe I should sleep the app like 3-5 seconds then exit

Also I dont use linger time, it is not so importnat for my application if any data is pending in the buffers....

This topic is closed to new replies.

Advertisement