I’m trying to read data from a socket, however whenever I try to read the entire stream my program hangs. With no error or exception.
I can manually read say 4 bytes and it will work, however I no longer know the exact size of the data the server will send, therefore I wish to read the entire stream. What am I doing wrong?
It hangs when calling ReadToEnd().
Stream input = socket.GetStream();
byte[] request = new byte[5];
input.Write(request, 0, 5); //send request
StreamReader reader = new StreamReader(input);
if (input.CanRead == true)
{
string test = reader.ReadToEnd();
}
Your code canot know how long the stream is, it’s possibly not ended so its going to continue to block until it has. Below is an example server and client (in no way is this a robust implementation) but if you consider the following code you should see how to send a request and receive a response:
You would need a client that did something like this