Hi there,
I'm playing with Socket and Telnet (via hyperterminal). I'm trying to
display a message when the client disconnect, that is when hyperterminal
either close the connection, or is closed, on the client side. But,
Socket.Connected always seems to return true, even though I do as in the doc
and do a 0 byte Send before checking its value.

Is there another way to know if a Socket is still connected to a client?

Etienne Fortin

Re: Socket.Connected by Markus

Markus
Wed Jul 20 16:07:41 CDT 2005

Etienne Fortin wrote:

> Hi there,
> I'm playing with Socket and Telnet (via hyperterminal). I'm trying to
> display a message when the client disconnect, that is when hyperterminal
> either close the connection, or is closed, on the client side. But,
> Socket.Connected always seems to return true, even though I do as in the
> doc and do a 0 byte Send before checking its value.
>
> Is there another way to know if a Socket is still connected to a client?

Try to do a receive on the socket. It normally returns the number of bytes
received.. but when the connection has been closed it will return 0 bytes.

hth,
Max

Re: Socket.Connected by EtienneFortin

EtienneFortin
Thu Jul 21 07:16:12 CDT 2005

Thanks for your answer.

It is a probably a good way to know if a connection is dead. But what about
the situation when the client didn't send any data for a certain period of
time, but it is still connected and alive? Receive() will return zero bytes.

Etienne


"Markus Stoeger" wrote:

> Etienne Fortin wrote:
>
> > Hi there,
> > I'm playing with Socket and Telnet (via hyperterminal). I'm trying to
> > display a message when the client disconnect, that is when hyperterminal
> > either close the connection, or is closed, on the client side. But,
> > Socket.Connected always seems to return true, even though I do as in the
> > doc and do a 0 byte Send before checking its value.
> >
> > Is there another way to know if a Socket is still connected to a client?
>
> Try to do a receive on the socket. It normally returns the number of bytes
> received.. but when the connection has been closed it will return 0 bytes.
>
> hth,
> Max
>

Re: Socket.Connected by Markus

Markus
Fri Jul 22 16:35:50 CDT 2005

Etienne Fortin wrote:

> It is a probably a good way to know if a connection is dead. But what
> about the situation when the client didn't send any data for a certain
> period of time, but it is still connected and alive? Receive() will return
> zero bytes.

If you are using blocking sockets, Receive will not return 0 bytes unless
the client disconnected. If you call Receive but there is no data
available, Receive will block and wait until there is data available or
until the client disconnects (in which case it would return 0 bytes).

Max