TCPListener - Client Disconnect

G

Guest

I recently upgraded my application from VB6, and was using the Winsock
control. In VB2005 I am using the TcpListener Class and have managed to get
it working, except for one thing:

Is there a way for the server to detect when a client disconnects (i.e.
closes the program abruptly)? In VB6 the Winsock control had the Close event,
which would fire when the client disconnected.

Some suggest that it can be done by sending a message to the client at
regular intervals (heartbeat) to check for connectivity, but I would rather
not have to do this.

Is there a method for when the client disconnects?
 
A

Armin Zingler

Hiwj said:
I recently upgraded my application from VB6, and was using the
Winsock control. In VB2005 I am using the TcpListener Class and have
managed to get it working, except for one thing:

Is there a way for the server to detect when a client disconnects
(i.e. closes the program abruptly)? In VB6 the Winsock control had
the Close event, which would fire when the client disconnected.

Some suggest that it can be done by sending a message to the client
at regular intervals (heartbeat) to check for connectivity, but I
would rather not have to do this.

Is there a method for when the client disconnects?



An event? I don't think so. More:

http://groups.google.com/groups/search?q=winsock+"close+event"+group:microsoft.public.dotnet.*



Armin
 
G

Guest

when a client disconnects, the number of bytes sent is 0....for example

private sub BytesReceived(ByVal ar As IAsycResult)

Dim numBytes as integer = ClientSocket.EndReceive(ar

if numBytes = 0

''close connection, client disconnected

....

end sub


in the method where you receive information, create a check like that and
you will be notified when a client disconnects

hope this helps
 
S

Sean

Ok, but what if the client does not send a constant stream data to the server? - such as in the case of a client command prompt? How then can you tell when the client disconnects?

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
G

Guest

since its a TCP connection, you dont have to worry about a constant stream of
bytes. whenever the connection is broken, the Received method will be called
with the number of bytes being 0. thats how you can tell if the client
disconnects
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top