读取操作期间的 C# TCPClient SocketError 10051。适配器 IP 为空
C# TCPClient SocketError 10051 during read operation. Adapters IP is empty
我有一个应用程序,它使用 TCPClient 连接到 TCPServer 并每 300 毫秒交换一次数据。应用程序和连接按预期工作。我测试了 2 天的连接,并且一直运行,没有任何错误。今天我遇到了一个我无法解决且不理解的错误。
计算机(TCPClient)和 TCPServer 在同一个网络中,并且都通过 DHCP 获取它们的 IP 地址。
应用程序从读取网卡信息开始,读取网卡ip地址等信息:
NetworkInterface[] adapter = NetworkInterface.GetAllNetworkInterfaces();
在计算机上发现两个适配器,下面的代码传递IP地址:
foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
this.ip = ip.Address.ToString();
this.mask = ip.IPv4Mask.ToString();
}
}
适配器一:IP:192.168.0.153,掩码:255.255.255.0
适配器二:IP:127.0.0.1,掩码:255.0.0.0(名称 Loopback Pseudo-Interface1)
我知道我的服务器的 IP 地址并开始连接。
controller = new TcpClient();
controller.Connect(192.168.0.0, 5001);
.... //Continues when controller.Connected == true
static NetworkStream ns_controller = controller.GetStream();
ns_controller.WriteTimeout = 100;
ns_controller.ReadTimeout = 10;
之后,我的应用程序在 800 秒内写入和读取网络流数据。然后一个新的读取指令在catch块结束:
try
{
byte[] r = new byte[100];
int anz = 0;
anz = ns_controller.Read(r, 0, r.Length);
}
catch (Exception ex)
{
//Output of the inner exception data and reset of communication
}
InnerException.ErrorCode: 10051 和 InnerException.Message: 套接字操作引用了不可用的网络
当我由于错误重置通信时,网络适配器在异常发生 100 毫秒后再次读取,代码与开始时相同。唯一的区别是 ip 地址和网络掩码现在是 "" 空的。
之后我关闭了我的应用程序并再次尝试。网络接口的ip地址又是192.168.0.153,没有变。
我想问一下以前有没有人遇到过这个问题,问题的原因是什么?我只发现了一些关于连接到服务器时发生的错误代码 10051 的问题。为什么controller的ip地址可以短时间为空,然后又是原来的值?
我知道我无法提供所有信息,但我希望这就足够了。问题是提供的信息都是我在协议中记录的。我无法再次强制错误以记录完整信息。欢迎任何帮助或提示。
我遇到的问题不完全相同,但与此类似,我通过找到根本原因解决了它。您可以尝试按照它进行操作,它可能会对您有所帮助,或者至少可以为您提供一些线索来解决您的问题:
使用 ScoketExeption 再添加一个 catch 块 所有可能的 套接字错误
try
{
//Your code .................
Socket socket = controller.Client; // not necessary to use but could be helpful to gracefully close the socket and for retry connection in catch( ) e.g. if(socket!=null) socket.close();
// your code..................
}
catch(SocketException se)
{
string exception = string.Empty;
switch (se.SocketErrorCode)
{
case SocketError.Success:
exception = "Success";
break;
case SocketError.SocketError:
exception = "Socket Error";
break;
case SocketError.Interrupted:
exception = "Socket Interrupted";
break;
case SocketError.AccessDenied:
exception = "Access Denied";
break;
case SocketError.Fault:
exception = "Socket Falut";
break;
case SocketError.InvalidArgument:
exception = "Invalid Argument";
break;
case SocketError.TooManyOpenSockets:
exception = "Too Many Open Sockets";
break;
case SocketError.WouldBlock:
exception = "Socket Blocked";
break;
case SocketError.InProgress:
exception = "In progress";
break;
case SocketError.AlreadyInProgress:
exception = "Already in progress";
break;
case SocketError.NotSocket:
exception = "Not Socket";
break;
case SocketError.DestinationAddressRequired:
exception = "Destination Address Required";
break;
case SocketError.MessageSize:
exception = "Message Size Error";
break;
case SocketError.ProtocolType:
exception = "Protocol Type Error";
break;
case SocketError.ProtocolOption:
exception = "Protocol Option Error";
break;
case SocketError.ProtocolNotSupported:
exception = "Protocol Not Supported";
break;
case SocketError.SocketNotSupported:
exception = "Socket Not Supported";
break;
case SocketError.OperationNotSupported:
exception = "Operation Not Supported";
break;
case SocketError.ProtocolFamilyNotSupported:
exception = "Protocol Family Not Supported";
break;
case SocketError.AddressFamilyNotSupported:
exception = "Address Family Not Supported";
break;
case SocketError.AddressAlreadyInUse:
exception = "Adreess Already In Use";
break;
case SocketError.AddressNotAvailable:
exception = "Address Not Avaialble";
break;
case SocketError.NetworkDown:
exception = "Network Down";
break;
case SocketError.NetworkUnreachable:
exception = "Network Unreachable";
break;
case SocketError.NetworkReset:
exception = "Network Reset";
break;
case SocketError.ConnectionAborted:
exception = "Connection Aborted";
break;
case SocketError.ConnectionReset:
exception = "Connection Reset";
break;
case SocketError.NoBufferSpaceAvailable:
exception = "No Buffer Space Available";
break;
case SocketError.IsConnected:
exception = "Connected";
break;
case SocketError.NotConnected:
exception = "Not Connected";
break;
case SocketError.Shutdown:
exception = "Shutdown";
break;
case SocketError.TimedOut:
exception = "Timed Out";
break;
case SocketError.ConnectionRefused:
exception = "Connection Refused";
break;
case SocketError.HostDown:
exception = "Host Down";
break;
case SocketError.HostUnreachable:
exception = "Host Unreachable";
break;
case SocketError.ProcessLimit:
exception = "Process Limit";
break;
case SocketError.SystemNotReady:
exception = "System Not Ready";
break;
case SocketError.VersionNotSupported:
exception = "Version Not Supported";
break;
case SocketError.NotInitialized:
exception = "Not Initialized";
break;
case SocketError.Disconnecting:
exception = "Disconnecting";
break;
case SocketError.TypeNotFound:
exception = "Type Not Found";
break;
case SocketError.HostNotFound:
exception = "Host Not Found";
break;
case SocketError.TryAgain:
exception = "Try Again";
break;
case SocketError.NoRecovery:
exception = "No Recovery";
break;
case SocketError.NoData:
exception = "No Data";
break;
case SocketError.IOPending:
exception = "IO Pending";
break;
case SocketError.OperationAborted:
exception = "Operation Aborted";
break;
default:
exception = "Un Specified Error";
break;
}
}
我有一个应用程序,它使用 TCPClient 连接到 TCPServer 并每 300 毫秒交换一次数据。应用程序和连接按预期工作。我测试了 2 天的连接,并且一直运行,没有任何错误。今天我遇到了一个我无法解决且不理解的错误。 计算机(TCPClient)和 TCPServer 在同一个网络中,并且都通过 DHCP 获取它们的 IP 地址。 应用程序从读取网卡信息开始,读取网卡ip地址等信息:
NetworkInterface[] adapter = NetworkInterface.GetAllNetworkInterfaces();
在计算机上发现两个适配器,下面的代码传递IP地址:
foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
this.ip = ip.Address.ToString();
this.mask = ip.IPv4Mask.ToString();
}
}
适配器一:IP:192.168.0.153,掩码:255.255.255.0 适配器二:IP:127.0.0.1,掩码:255.0.0.0(名称 Loopback Pseudo-Interface1)
我知道我的服务器的 IP 地址并开始连接。
controller = new TcpClient();
controller.Connect(192.168.0.0, 5001);
.... //Continues when controller.Connected == true
static NetworkStream ns_controller = controller.GetStream();
ns_controller.WriteTimeout = 100;
ns_controller.ReadTimeout = 10;
之后,我的应用程序在 800 秒内写入和读取网络流数据。然后一个新的读取指令在catch块结束:
try
{
byte[] r = new byte[100];
int anz = 0;
anz = ns_controller.Read(r, 0, r.Length);
}
catch (Exception ex)
{
//Output of the inner exception data and reset of communication
}
InnerException.ErrorCode: 10051 和 InnerException.Message: 套接字操作引用了不可用的网络
当我由于错误重置通信时,网络适配器在异常发生 100 毫秒后再次读取,代码与开始时相同。唯一的区别是 ip 地址和网络掩码现在是 "" 空的。 之后我关闭了我的应用程序并再次尝试。网络接口的ip地址又是192.168.0.153,没有变。
我想问一下以前有没有人遇到过这个问题,问题的原因是什么?我只发现了一些关于连接到服务器时发生的错误代码 10051 的问题。为什么controller的ip地址可以短时间为空,然后又是原来的值?
我知道我无法提供所有信息,但我希望这就足够了。问题是提供的信息都是我在协议中记录的。我无法再次强制错误以记录完整信息。欢迎任何帮助或提示。
我遇到的问题不完全相同,但与此类似,我通过找到根本原因解决了它。您可以尝试按照它进行操作,它可能会对您有所帮助,或者至少可以为您提供一些线索来解决您的问题:
使用 ScoketExeption 再添加一个 catch 块 所有可能的 套接字错误
try
{
//Your code .................
Socket socket = controller.Client; // not necessary to use but could be helpful to gracefully close the socket and for retry connection in catch( ) e.g. if(socket!=null) socket.close();
// your code..................
}
catch(SocketException se)
{
string exception = string.Empty;
switch (se.SocketErrorCode)
{
case SocketError.Success:
exception = "Success";
break;
case SocketError.SocketError:
exception = "Socket Error";
break;
case SocketError.Interrupted:
exception = "Socket Interrupted";
break;
case SocketError.AccessDenied:
exception = "Access Denied";
break;
case SocketError.Fault:
exception = "Socket Falut";
break;
case SocketError.InvalidArgument:
exception = "Invalid Argument";
break;
case SocketError.TooManyOpenSockets:
exception = "Too Many Open Sockets";
break;
case SocketError.WouldBlock:
exception = "Socket Blocked";
break;
case SocketError.InProgress:
exception = "In progress";
break;
case SocketError.AlreadyInProgress:
exception = "Already in progress";
break;
case SocketError.NotSocket:
exception = "Not Socket";
break;
case SocketError.DestinationAddressRequired:
exception = "Destination Address Required";
break;
case SocketError.MessageSize:
exception = "Message Size Error";
break;
case SocketError.ProtocolType:
exception = "Protocol Type Error";
break;
case SocketError.ProtocolOption:
exception = "Protocol Option Error";
break;
case SocketError.ProtocolNotSupported:
exception = "Protocol Not Supported";
break;
case SocketError.SocketNotSupported:
exception = "Socket Not Supported";
break;
case SocketError.OperationNotSupported:
exception = "Operation Not Supported";
break;
case SocketError.ProtocolFamilyNotSupported:
exception = "Protocol Family Not Supported";
break;
case SocketError.AddressFamilyNotSupported:
exception = "Address Family Not Supported";
break;
case SocketError.AddressAlreadyInUse:
exception = "Adreess Already In Use";
break;
case SocketError.AddressNotAvailable:
exception = "Address Not Avaialble";
break;
case SocketError.NetworkDown:
exception = "Network Down";
break;
case SocketError.NetworkUnreachable:
exception = "Network Unreachable";
break;
case SocketError.NetworkReset:
exception = "Network Reset";
break;
case SocketError.ConnectionAborted:
exception = "Connection Aborted";
break;
case SocketError.ConnectionReset:
exception = "Connection Reset";
break;
case SocketError.NoBufferSpaceAvailable:
exception = "No Buffer Space Available";
break;
case SocketError.IsConnected:
exception = "Connected";
break;
case SocketError.NotConnected:
exception = "Not Connected";
break;
case SocketError.Shutdown:
exception = "Shutdown";
break;
case SocketError.TimedOut:
exception = "Timed Out";
break;
case SocketError.ConnectionRefused:
exception = "Connection Refused";
break;
case SocketError.HostDown:
exception = "Host Down";
break;
case SocketError.HostUnreachable:
exception = "Host Unreachable";
break;
case SocketError.ProcessLimit:
exception = "Process Limit";
break;
case SocketError.SystemNotReady:
exception = "System Not Ready";
break;
case SocketError.VersionNotSupported:
exception = "Version Not Supported";
break;
case SocketError.NotInitialized:
exception = "Not Initialized";
break;
case SocketError.Disconnecting:
exception = "Disconnecting";
break;
case SocketError.TypeNotFound:
exception = "Type Not Found";
break;
case SocketError.HostNotFound:
exception = "Host Not Found";
break;
case SocketError.TryAgain:
exception = "Try Again";
break;
case SocketError.NoRecovery:
exception = "No Recovery";
break;
case SocketError.NoData:
exception = "No Data";
break;
case SocketError.IOPending:
exception = "IO Pending";
break;
case SocketError.OperationAborted:
exception = "Operation Aborted";
break;
default:
exception = "Un Specified Error";
break;
}
}