SocketConnected question?

BlitzMax Forums/BlitzMax Programming/SocketConnected question?

*(Posted 2008) [#1]
Is SocketConnected a good way to see if clients are in the game, I ask because in my networking system I use SocketConnected to test to see if people have left (the client then just has to close thier socket). The problem I am having is when one client closes the server tells me that ALL clients have closed thier sockets and kills all of em.

I used to have a network message telling me that a client disconnected and then the client waited for the acknowledgement from the server but this took about two to three seconds to disconnect, so I thought ah SocketConnected can tell me if the client has killed thier socket but it kills everyone.


rdodson41(Posted 2008) [#2]
SocketConnected isn't really a great way to detect if a socket has actually been closed. The only real way to actually and safely tell if a socket has been closed is to try to read from or write to it and have an exception thrown:
Try
    socket.Send(buffer, length)
    socket.Recv(buffer, length)
Catch o:Object
    Print "Socket has been closed."
EndTry

This is because SocketConnected returns true if the socket is still open OR it is closed but there is data in the buffer. So to detect immediate socket closing SocketConnected is not the best method, so try the above.


*(Posted 2008) [#3]
Thanks will use that :)