networkstream

How do I determine when there is no more data to read in a NetworkStream?

I have a web app which connects to a server using a TCP connection and reads a binary document which it then writes to its response object. In other words it's transferring a file from a backend server using a custom protocol and returning that file to its client through HTTP. The server sends a status code and a mime type, which I read...

Does NetworkStream.DataAvailable see buffered data?

Does NetworkStream.DataAvialable know whether the sender's send buffer is empty? Or does it simply indicate whether the receiver's read buffer has data? My assumption is the latter... Specifically, for some socket work involving an ongoing conversation, I currently use a length-prefix so the the receiver knows exactly how much data is i...

Counterpart of .NETs NetworkStream / SslStream in Delphi 7

I have written a secure TCP server in .NET. This was basically as simple as creating a TcpListener instance and wrapping the connected client's NetworkStreams with SslStreams. Now I need to access this TCP server with Delphi 7 (alternatively: Delphi 2007). I haven't found anything in the help, and a Google search shows up lots of compl...

C# Read Line from Byte Array (Not Convert Byte Array to String)

I have a byte array that I am reading in from a NetworkStream. The first two bytes tell the length of the packet that follows and then the packet is read into a byte array of that length. The data in that I need to read from the NetworkStream/byte array has a few Strings, i.e. variable length data terminated by new line characters, and s...

C# NetworkStream.Read()

I'd like to empty read buffer of the socket so I wrote follow code... byte[] tempBuffer = new byte[1024]; int readCount = 0; while ((readCount = tcpSocket.GetStream().Read(tempBuffer, 0, tempBuffer.Length)) != 0) { // do with tempBuffer } But Read() method is blocked so I added tcpSocket.ReceiveTimeout = 1;. And it works just like...

.NET NetworkStream Read slowness

I've got some network code to process an arbitary TCP connection. It all seems to work as expected but seems slow. When i've profiled the code the it seems to spend a good 600 ms in NetworkStream.Read() and I'm wondering how to improve it. I've fiddled with the buffer sizes and alternated between a massive buffer to read all of the dat...

System.Net.Sockets.NetworkStream's Async read callback

I'm may be just misunderstanding something fundamental here but... Senario: I call System.Net.Sockets.NetworkStream's BeginRead method and my machine receives a response/request from a network device. The runtime runs my callback in its own thread. Before this thread can call EndRead, the machine receives another response/request. Ques...

Running Async IO threads to completion in same order as received

Sorry, I am very new to all this multithreading stuff... I'm working on a client/server app and I'm going to use System.Net.Sockets.NetworkStream's Async IO methods. I'm aware that after calling BeginRead, the system will start calling my callback every time it receives data. The callback could take a significant amount of time to co...

NetworkStream, is there something similar to DataReceived for a SerialPort? (C#)

Ok, so I'm a little confused as to why I can't find this anywhere, or if it doesn't exist then why have Microsoft not implemented it? So here's my scenario, I have a NetworkStream, which has a lovely little boolean called DataAvailable, and what I need is an event, that jumps out and says "Hey, there's data available for you!" (because ...

Re-opening a closed NetworkStream?

Good morning, I am in need of some advice, I am using a networkStream, which is streaming from a TcpClient, all was working fine, but then I required the some functionality elsewhere, which requires the stream to be closed. I can't seem to find a way to re open the connection once it has been closed. Could anyone point me in the correct...

How do you wait for a Network Stream to have data to read?

I have a worker thread in my application that is responsible for three different things. Requests for two of the jobs turn up in Queues that I have written, the other job is activated when a request turns up on a Network stream. I would like my worker thread to wait when there is no work to be done. This is easy with the two Queues as th...

How to check if TcpClient Connection is closed?

I'm playing around with the TcpClient and I'm trying to figure out how to make the Connected property say false when a connection is dropped. I tried doing NetworkStream ns = client.GetStream(); ns.Write(new byte[1], 0, 0); But it still will not show me if the TcpClient is disconnected. How would you go about this using a TcpClient? ...

StreamWriter won't flush to NetworkStream

Using a StreamWriter to write to a NetworkStream, and a StreamReader to read the response. The app is sending commands and reading responses to a news server. Simplified code (sans error handling, etc.): tcpClient = new TcpClient(); tcpClient.Connect(Name, Port); networkStream = tcpClient.GetStream(); serverReader = new StreamReader(...

networkStream.CanRead true but Buffer returning no value

I have a do while loop that reads a buffer from a NetworkStream object the while condition is networkStream.CanRead so as long as it can read it should continue reading from the buffer. Only problem is when I read from the buffer and convert to string, there is nothing in it . i.e. its empty. Why would that happen? This is an ASP.net (...

How can I recreate an error where Network.CanRead is true and nothing is being sent

Related Question: http://stackoverflow.com/questions/1746342/networkstream-canread-true-but-buffer-returning-no-value I want to recreate a TCP server that doesn't send any data on the network stream (or that's how it seems when the bytes are converted to string) but the NetworkStream object .CanRead property still remains true If I don...

[C#] What is the correct way to close a TCP connection

I have a TcpClient object which sends some data to server, using its underlying NetworkStream.Write(). Therefor, I have: TcpClient server = new TcpClient(serverName, 50001); /* ... */ NetworkStream stream = server.GetStream(); Now, when a button is pressed, the connection should close. What is the right way to close the connection? ...

Detecting client TCP disconnection while using NetworkStream class

A friend of mine came to me with a problem: when using the NetworkStream class on the server end of the connection, if the client disconnects, NetworkStream fails to detect it. Stripped down, his C# code looked like this: List<TcpClient> connections = new List<TcpClient>(); TcpListener listener = new TcpListener(7777); listener.Start()...

NetworkStream.Read delay .Net

I have a class that inherits from TcpClient. In that class I have a method to process responses. In that method I call I get the NetworkStream with MyBase.GetStream and call Read on it. This works fine, excpet the first call to read blocks too long. And by too long I mean that the socket has recieved plenty of data, but won't read it un...

TcpClient and StreamReader blocks on Read

Here's my situation: I'm writing a chat client to connect to a chat server. I create the connection using a TcpClient and get a NetworkStream object from it. I use a StreamReader and StreamWriter to read and write data back and forth. Here's what my read looks like: public string Read() { StringBuilder sb = new StringBuilder(); try...

C#: Alternative to NetworkStream.Read that indicates remote host has closed the connection?

With regards to handling a TCP/IP connection using the TcpClient class, is there an alternative for checking whether the remote host has closed the connection other than waiting for the NetworkStream.Read method to return a 0? ...