Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅在windows 10中出现网络流读取错误_C#_Sockets_Tcpclient_Networkstream_System.net.sockets - Fatal编程技术网

C# 仅在windows 10中出现网络流读取错误

C# 仅在windows 10中出现网络流读取错误,c#,sockets,tcpclient,networkstream,system.net.sockets,C#,Sockets,Tcpclient,Networkstream,System.net.sockets,我遇到了一个奇怪的错误,我只尝试写入和读取一个流缓冲区,但当尝试读取它时引发了SocketException 这是我的代码: // create tcp client System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient("<localhost>", <port>); client.ReceiveTimeout = 1; // timeout 1sec // get stream t

我遇到了一个奇怪的错误,我只尝试写入和读取一个流缓冲区,但当尝试读取它时引发了SocketException

这是我的代码:

// 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
//创建tcp客户端
System.Net.Sockets.TcpClient client=新的System.Net.Sockets.TcpClient(“,”);
client.ReceiveTimeout=1;//超时1秒
//获取可用于读/写的流
System.Net.Sockets.NetworkStream=client.GetStream();
//准备请求消息
String request=“我的消息”;
byte[]requestBytes=System.Text.Encoding.ASCII.GetBytes(请求);
stream.Write(requestBytes,0,requestBytes.Length);
//获取响应消息
字节[]响应字节缓冲区=新字节[1000];
int bytesRead=stream.Read(responseByteBuffer,0,1000)//这里提出袜子例外
错误:

未处理的异常:System.IO.IOException:无法从中读取数据 传输连接:连接尝试失败,因为 关联方在一段时间后未作出适当回应,或 建立的连接失败,因为连接的主机无法连接 回应。-->System.Net.Sockets.SocketException:连接尝试 失败,因为关联方在发生错误后没有正确响应 一段时间,或由于已连接而建立的连接失败 主机未能在上响应 System.Net.Socket.Socket.Receive(字节[]缓冲区,Int32偏移量,Int32 尺寸,SocketFlags(SocketFlags)位于 System.Net.Sockets.NetworkStream.Read(字节[]缓冲区,Int32偏移量, Int32大小)--内部异常堆栈跟踪的结束---at System.Net.Sockets.NetworkStream.Read(字节[]缓冲区,Int32偏移量, Int32大小)

但奇怪的是,在Windows7上使用相同网络环境的相同代码可以正常工作。但当我尝试在Windows 10中运行该工具时崩溃。。。不可用


我尝试了很多方法:增加接收和读取超时,关闭防火墙,但行为相同。我也使用Visual Studio 2015中的网络跟踪功能,但同样的错误和没有提示…

您找到解决方案了吗?