detectc client disconnect
helo guys
i wrote a program (vb.net2005) for client- server communications using tcp/ip ,all are workin pretty well except one thing . when ever one new client is conected to server i ll display client ip and machine name on a grid. but i wont able to detect when a client is disconnected from a server
please provide some sample code for this:wave:
thx
Re: detectc client disconnect
You need to test whether the socket is still connected, then cleanup from there.
Do you know how to make custom events?
Re: detectc client disconnect
no campion,
i dont no how to make custom events can u help me out
pls and also pls specify some sample codes
tahnx
Re: detectc client disconnect
I need a little more information, as well. Are you using an object to encapsulate the connections, or are they just being handled by a form?
Is each connection handled inside of its own class?
Re: detectc client disconnect
yep it is just handled by a form . i had one client module and server module
to handle connections
Re: detectc client disconnect
This is how I handle disconnections. There's an event in there that fires off when it is sensed that the socket is no longer connected. However, since you have everything self-contained onto a form, what you need to do instead is ensure that your sockets are closed and that any references to the client no longer exists anywhere else in your project.
Code:
Try
BytesRead = _TcpServer.GetStream.EndRead(Ar)
_ServerMsgBuild = Encoding.GetEncoding(1252).GetString(_ServerBuffer, 0, BytesRead)
While _TcpServer.GetStream.DataAvailable
_ServerMsgBuild += Encoding.GetEncoding(1252).GetString(_ServerBuffer, 0, _
_TcpServer.GetStream.Read(_ServerBuffer, 0, _ServerBuffer.Length))
End While
Catch ex As Exception
RaiseEvent Disconnected()
Return
End Try