DataAvailable property in TCPClient class misleading in usage

J

John Mott

Hello,

I'm not really sure if this is the best place but I couldn't find a TCP
specific newsgroup.

It appears that the DataAvailable property of the TCPClient is just a test
for whether data is buffered, and does not take into account whether the
socket to the server is still open. While this is technically accurate, data
either is or is not buffered, its not as useful as knowing whether or not
the socket is still open or not.

The sample code in MSDN has a loop testing on DataAvailable, which will
produce a false negative if the server sends data in bursts with delays. It
is useful to know that data is buffered, but its incomplete knowledge. There
is a property on the TCPClient called 'Connected' which be tested as well.

My solution in my sample code is to use the ReadByte(), which is less
efficient but will return -1 when the socket is closed.

http://www.nicecleanexample.com/ViewSnippet.aspx?TID=dotnet_tcpclient1

Thanks,

John
 
B

bruce barker

http is a stream protocol over tcp/ip. there is no notification of
closed sockets or heartbeats. its a standard ack/nak protocol. when the
last data is sent, a fin is sent. if the socket is closed, nothing is
sent. the receiver detects a closed socket via a timeout. if the
connection is closed during a data packet send, the timeout is quick. if
between packet sends, then its very long.

because tcp is a reliable protocol, packets are delivered in order, and
the sender is notified of receipt, it is always buffered, as ip is not
reliable and will deliver packets out of order.

your code does not really change anything. reading the steam shoudl
return an error as quick as readbytes, which is just a read of buffer
size one. if you want more control, look at the socket library.

-- bruce (sqlwork.com)
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top