ReadToEnd() Blocking

B

bernardpace

Hi,

I am writing to a network stream using the following code:

TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse("192.168.2.100"), 55650);
NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write("TEST");
writer.Flush();

Now, I am reading from the network stream using the following code:

NetworkStream netStrm = new NetworkStream(socket);
StreamReader reader = new StreamReader(netStrm);
string str = reader.ReadToEnd();

The method ReadToEnd(), is blocking the client application is
terminated. I also tried to client.Close(), but still method blocks.

Is there a way to read all data sent without terminating the client.


Can someone help me out
Thanks in Advance
 
J

Jon Skeet [C# MVP]

I am writing to a network stream using the following code:

TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse("192.168.2.100"), 55650);
NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write("TEST");
writer.Flush();

Now, I am reading from the network stream using the following code:

NetworkStream netStrm = new NetworkStream(socket);
StreamReader reader = new StreamReader(netStrm);
string str = reader.ReadToEnd();

The method ReadToEnd(), is blocking the client application is
terminated. I also tried to client.Close(), but still method blocks.

Is there a way to read all data sent without terminating the client.

No - there's no real sense of "all data" until the stream has been
closed, because the server could always have sent some more since you
thought you'd got it all.

If you're able to design the protocol yourself, it's a *really* good
idea to state how much data you're going to send before you send it
wherever there's a variable amount.
 
R

Rob Schieber

Hi,

I am writing to a network stream using the following code:

TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse("192.168.2.100"), 55650);
NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write("TEST");
writer.Flush();

Now, I am reading from the network stream using the following code:

NetworkStream netStrm = new NetworkStream(socket);
StreamReader reader = new StreamReader(netStrm);
string str = reader.ReadToEnd();

The method ReadToEnd(), is blocking the client application is
terminated. I also tried to client.Close(), but still method blocks.

Is there a way to read all data sent without terminating the client.


Can someone help me out
Thanks in Advance

Have you looked into the Asynchronous methods such as BeginRead,
BeginWrite etc...? Thes operations are executed on a background thread
and shouldn't block the apps main thread.
 
W

William Stacey [MVP]

client.shutdown(send) (or shutdown(both) ) from the client will unblock
server blocking read and read will return 0. Even if you "packetize" your
stream with pre-pended len bytes, you most likely need to do this anyway to
signal to the server that your not sending any more messages.
 

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