Archived

This topic is now archived and is closed to further replies.

ANSI2000

Closing a socket

Recommended Posts

ANSI2000    122
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()?

Share this post


Link to post
Share on other sites
Fruny    1658
Fistly, enable the server to reuse the socket address : before bind(), do :

int param = 1;
setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, ¶m, 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 ]

Share this post


Link to post
Share on other sites
ANSI2000    122
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....

Share this post


Link to post
Share on other sites