471,310 Members | 1,116 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Problem with application trying to read data from a network streamwhen the socket is closed

I have a function within an application where a client connected to a
server continuously sends data. The code on the server side is of the
following form:

Socket socket = receiveSocket;
NetworkStream networkStream = new NetworkStream (socket);
System.IO.StreamReader streamReader = new System.IO.StreamReader
(networkStream);

while (socket.Connected)
{
streamReader.ReadLine();
}

// Do something else when no longer connected
I would like the server to stop reading from the buffer if the client
closes the connection. Instead, the server continues to read blank
lines continuously. No exceptions are thrown. How can I stop this
behaviour and obtain the behaviour that I would like?
Dec 13 '06 #1
2 2628
White Spirit,

I find that when it comes to socket-based streams, you can't use stream
readers to do this. They try and buffer bytes from the stream, no matter
what the underlying stream is. Since the socket disconnects, and you call
ReadLine, the StreamReader hangs on the call to Read on the stream, waiting
for the new line combo.

I recommend using the NetworkStream, and reading byte by byte yourself,
parsing apart the new line on your own. If the socket disconnects, then no
more bytes will be returned to you (if the socket is closed).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"White Spirit" <ws*****@homechoice.co.ukwrote in message
news:45********@news1.homechoice.co.uk...
>I have a function within an application where a client connected to a
server continuously sends data. The code on the server side is of the
following form:

Socket socket = receiveSocket;
NetworkStream networkStream = new NetworkStream (socket);
System.IO.StreamReader streamReader = new System.IO.StreamReader
(networkStream);

while (socket.Connected)
{
streamReader.ReadLine();
}

// Do something else when no longer connected
I would like the server to stop reading from the buffer if the client
closes the connection. Instead, the server continues to read blank lines
continuously. No exceptions are thrown. How can I stop this behaviour
and obtain the behaviour that I would like?

Dec 13 '06 #2
Nicholas Paldino [.NET/C# MVP] wrote:
I recommend using the NetworkStream, and reading byte by byte yourself,
parsing apart the new line on your own. If the socket disconnects, then no
more bytes will be returned to you (if the socket is closed).
That would solve the problem in terms of program execution. I don't
understand why Socket.Connected wouldn't return false in the example
code, though. In the program I'm writing, it will leave a thread
running unnecessarily and because of the way the data is expected to be
received, I can't use a timeout to close it.
Dec 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Rajarshi Guha | last post: by
3 posts views Thread by Patrick Olurotimi Ige | last post: by
3 posts views Thread by Dirk Reske | last post: by
2 posts views Thread by Giulio Petrucci | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.