NetworkStream read error only in windows 10

1k Views Asked by At

I faced an strange error I try only to write and read a stream buffer , but when trying to read it raise SocketException.

This is my code:

// create tcp client
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient("<localhost>", <port>);

client.ReceiveTimeout = 1; // timeout 1sec
// get stream that can be used for reading/writing

System.Net.Sockets.NetworkStream stream = client.GetStream();

// prepare the request message
String request ="My message";
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(request);
stream.Write(requestBytes, 0, requestBytes.Length);

// get the response message
byte[] responseByteBuffer = new byte[1000];
int bytesRead = stream.Read(responseByteBuffer, 0, 1000); //Here raise SocketException

Error:

Unhandled Exception: System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

But the strange thing is that the same code with the same network environment on Windows 7 is works fine. But when I try run the tool in Windows 10 crashes... unavailable

I tried a lot of things: increase receive and read timeout, turn off firewall but the same behavior . I also use Network trace from Visual Studio 2015 but the same error and no hints for me...

0

There are 0 best solutions below