[Hugo Ferreira][Positronic Dreams][Stick Soldiers]
"Please Review my WebSite!".
WinSock-Detecting Disconnects
Hi all,
how do I detect when a client disconnects (normally
or abnormally) from my server? I''m kindda new to this
send, receive, accept thing...
Thanx,
If you are using asynchronous sockets you can check for the FD_CLOSE message (it will come in the WM_USER+something message - whatever you defined it to be). The message will come with a Connection Reset By Peer error if the disconnect was abnormal. There are some good tutorials on GameDev about this in the articles section.
Firebird Entertainment
Firebird Entertainment
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
Im using normal bloking sockets, with threads...
[Hugo Ferreira][Positronic Dreams][Stick Soldiers]
"Please Review my WebSite!".
Im using normal bloking sockets, with threads...
[Hugo Ferreira][Positronic Dreams][Stick Soldiers]
"Please Review my WebSite!".
One way that''s done is to send a special PING message every so often (90 seconds is a commonly used number). The client then responds with a PONG message. If you''re reading to send out the next PING to a client, but they never responded to the last PING, you can assume they are disconnected.
- Andy Oxfeld
- Andy Oxfeld
This is off the top of my head (I haven''t used winsock in a while), but I believe you can check the return value of a call to read(). I think a socket tells you there is data to read, and then when you actually read it read() returns either 0 or something less than 0, and that means the client was disconnected. Sorry to be so vague, but look up read in MSDN and play around with it.
That''s correct Anthracks.
recv() returns 0 if the socket has been disconnected normally, or < 0 (SOCKET_ERROR) if disconnected abnormally (/other error). If the number is greater than zero, then the recv was successful.
Another thing to check is select(). Use select to determine if you should read a socket or not - if a disconnection has taken place, then select() will tell you that you should read the socket (via recv()) and you''ll discover the disconnection when recv returns <= 0.
Hope that helps.
Tom l
recv() returns 0 if the socket has been disconnected normally, or < 0 (SOCKET_ERROR) if disconnected abnormally (/other error). If the number is greater than zero, then the recv was successful.
Another thing to check is select(). Use select to determine if you should read a socket or not - if a disconnection has taken place, then select() will tell you that you should read the socket (via recv()) and you''ll discover the disconnection when recv returns <= 0.
Hope that helps.
Tom l
refrain_from_stupidity( &me );
What would shutdown(Socket, parameter) do?
Would this trigger a SOCKET_ERROR with WSAGetLastError() = WSAESHUTDOWN ?
and what is this graceful or unexcpected?
what would be the best way of gracefully close a client socket?
say bye to the server and call what?
thanks
Would this trigger a SOCKET_ERROR with WSAGetLastError() = WSAESHUTDOWN ?
and what is this graceful or unexcpected?
what would be the best way of gracefully close a client socket?
say bye to the server and call what?
thanks
It depends on the "linger" option of the client socket. Generally, calling
codeka.com - Just click it.
shutdown
will initiate a graceful shutdown. But if you set the "linger" option to 0, it''ll do an ungraceful shutdown and recv
will return < 0. You''d also get an ungraceful shutdown if, for example, you pulled the network cable out of the socket.WSAESHUTDOWN
is only returned if you call shutudown
on you own socket, so if you did something like this:recv( s, buffer, numbytes, 0 ); // this one is OK
shutdown( s, FD_RECEIVE );
recv( s, buffer, numbytes, 0 ); // this would return WSAESHUTDOWN
If I had my way, I''d have all of you shot!
codeka.com - Just click it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement