473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2725
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rajarshi Guha | last post by:
Hi, I'm writing some code using PyGTK2 that uses inpu_add_full() to watch a socket. I've included the code below. I have two questions: 1) What does a condition == 16 mean? It does match any of...
1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
0
by: Galore | last post by:
Hello, I'm trying to make a simple socket application, something like a chat application, but when I run it, it shows the following error when the client connects to the server: "Unable to read...
3
by: Patrick Olurotimi Ige | last post by:
With the code below i get error:- Invalid attempt to read data when reader is closed. //Get a datareader SqlDataReader objDataReader; objDataReader =...
4
by: Josema | last post by:
Hi to all, Im searching a hand to solve an Exception that i get when i try to give the user to a gmail account... Any help would be appreciated.. Thanks in advance This is a piece of my...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
2
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless...
3
by: Dirk Reske | last post by:
Hello, in msdn stands, that the socket.available property can fire a SocketException when the remote machine has closed the connection. why can? I have to check the number of receivable bytes...
5
by: Arno | last post by:
reposted with the right microsoft managed newsgroup ID: Sorry for the inconvinience Hi, I've written a class for client-socket connection, but I get a lot of times the error message "Unable...
2
by: Giulio Petrucci | last post by:
Hi everytbody, I'm experiencing some problems trying to asynchronously read data from a Socket object. What I do now is: 1) call th BeginReceive(...) method on the Socket object; 2) within the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.