简体   繁体   English

面临错误 tcp 侦听器 SocketException:{0}System.Net.Sockets.SocketException (0x80004005):请求的地址在其上下文中无效

[英]Facing error tcp listener SocketException: {0}System.Net.Sockets.SocketException (0x80004005): The requested address is not valid in its context

I developed a TCP Listener.我开发了一个 TCP 监听器。 Below is main coding of listener:下面是监听器的主要代码:

public static void GetXMLStream()
    {
       TcpListener lisetner = null;
        try
        {
            Int32 port = Int32.Parse(AppConfigValues.GetAppConfigValue("Port"));
            IPAddress localAddr = IPAddress.Parse(AppConfigValues.GetAppConfigValue("IPAddress"));
            lisetner = new TcpListener(localAddr, port);
            MIEventLogs.WriteLog("trying to Start");
            lisetner.Start();
            MIEventLogs.WriteLog("Started");
            while (true)
            {
                //TcpClient client = await server.AcceptTcpClientAsync(); //For future purpose only.
                //await Task.Run(async () => await ProcessClient(client));
                TcpClient client = lisetner.AcceptTcpClient();
                MIEventLogs.WriteLog("Accepted new client with IP : "+ ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString()+" Port: "+ ((IPEndPoint)client.Client.RemoteEndPoint).Port.ToString());
                ThreadPool.QueueUserWorkItem(ProcessClient, client);
            }
        }
        catch (SocketException e)
        {
            MIEventLogs.WriteLog("SocketException: {0}" + e);
        }
        finally
        {
            lisetner.Stop();
        }
    }

I ran this code this is working fine on local by passing IPAddress of system on which it is deployed.我通过传递部署它的系统的 IPAddress 运行了这段代码,这在本地运行良好。

Now we deployed this on one of our prod server it is also working on that prod server by passing IPAddress of that server on which it is deployed.现在我们将它部署在我们的一个产品服务器上,它也通过传递部署它的服务器的 IPAddress 在该产品服务器上工作。

Now we deployed this on another prod server by passing IPAddress of that server on which it is deployed, Here It is throwing error:现在我们通过传递部署它的服务器的 IPAddress 将它部署在另一个 prod 服务器上,这里抛出错误:

SocketException: {0}System.Net.Sockets.SocketException (0x80004005): The requested address is not valid in its context. SocketException:{0}System.Net.Sockets.SocketException (0x80004005):请求的地址在其上下文中无效。

This error always comes on lisetner.Start();这个错误总是出现在lisetner.Start();

Now I did below analysis:现在我做了以下分析:

  1. I gone through so many question like this, in most of them they provided work around by passing IPAddress as 0.0.0.0 or IPAddress.Any.我经历了很多这样的问题,在大多数问题中,他们通过将 IPAddress 传递为 0.0.0.0 或 IPAddress.Any 来提供解决方法。 It worked fine, How my concern is why IPAddress is not working fine.它工作正常,我担心的是为什么 IPAddress 不能正常工作。

  2. I checked Port binding by "netstat -nao|findstr 700", This port is not binded.我通过“netstat -nao|findstr 700”检查了端口绑定,该端口未绑定。

  3. I checked system ip by ipconfig it is same as passed in IPAddress.我通过 ipconfig 检查了系统 ip,它与在 IPAddress 中传递的相同。

  4. I checked ping by ping 10.83.77.254, ping is working fine.我通过 ping 10.83.77.254 检查了 ping,ping 工作正常。

My concern are why exact ip of server is not working and IPAddress.Any is working, On another hand other two server are working fine with IP Address, IPAddress.Any and 0.0.0.0 is我担心的是为什么服务器的确切 ip 不工作而 IPAddress.Any 工作正常,另一方面,其他两台服务器在 IP 地址、IPAddress.Any 和 0.0.0.0 上工作正常

I don't know why it won't work with the exact IP address of the server.我不知道为什么它不适用于服务器的确切 IP 地址。 It's possible you haven't entered the address correctly, or that it's a secondary address or something.可能您没有正确输入地址,或者它是辅助地址或其他东西。

Be that as it may, you should always use IPAddress.Any , or offer the user options from the list of connected network interfaces.尽管如此,您应该始终使用IPAddress.Any ,或者从连接的网络接口列表中提供用户选项。 You cannot bind to an interface that is not connected.您不能绑定到未连接的接口。

Also you should convert this to fully async code.此外,您应该将其转换为完全async代码。

public static async Task GetXMLStream(CancellationToken token)
{
    TcpListener listener = null;
    try
    {
        Int32 port = Int32.Parse(AppConfigValues.GetAppConfigValue("Port"));
        listener = new TcpListener(IPAddress.Any, port);
        MIEventLogs.WriteLog("trying to Start");
        listener.Start();
        MIEventLogs.WriteLog("Started");
        while (true)
        {
            TcpClient client = await listener.AcceptTcpClientAsync(token);
            MIEventLogs.WriteLog("Accepted new client with IP : "+ ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString()+" Port: "+ ((IPEndPoint)client.Client.RemoteEndPoint).Port.ToString());
            Task.Run(async () => await ProcessClient(client, token));
                        // make sure ProcessClient disposes the client
        }
        catch (SocketException e)
        {
            MIEventLogs.WriteLog("SocketException: {0}" + e);
        }
        catch (OperationCanceledException)
        { //
        }
        finally
        {
            listener.Stop();
        }
    }
}

In my case, I did more analysis and found that this exception only comes when I restart system and Service Startup Type is automatic.在我的情况下,我做了更多的分析,发现这个异常只有在我重新启动系统并且服务启动类型为自动时才会出现。 If I change Service Startup Type Automatic to Manual and Restart system, It was always works fine.如果我将服务启动类型自动更改为手动并重新启动系统,它总是可以正常工作。 So in nutshell, It is related to IP Address assignment.所以简而言之,它与 IP 地址分配有关。 As on starting Service gets started immediately, however network connection in turn IP Assignment takes time.随着启动服务立即启动,但是网络连接反过来 IP 分配需要时间。 Now I set Startup Type Automatic (Delayed Start) and It is working fine.现在我设置了启动类型自动(延迟启动),它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Net.Sockets.SocketException (0x80004005): 没有这样的主机是已知的 - System.Net.Sockets.SocketException (0x80004005): No such host is known System.Net.Sockets.SocketException(0x80004005) - System.Net.Sockets.SocketException (0x80004005) System.Net.Sockets.SocketException(0x80004005):建立的连接已被主机中的软件中止 - System.Net.Sockets.SocketException (0x80004005): An established connection was aborted by the software in your host machine TcpListener不适用于其他机器ip:System.Net.Sockets.SocketException(0x80004005) - TcpListener not working for other machine ip: System.Net.Sockets.SocketException (0x80004005) System.Net.Sockets.SocketException - System.Net.Sockets.SocketException SFTP连接DNS问题-System.Net.Sockets.SocketException:请求的名称有效,但是未找到请求类型的数据 - SFTP connectivity DNS issue - System.Net.Sockets.SocketException: The requested name is valid, but no data of the requested type was found “请求的地址在其上下文中无效” SocketException - “The requested address is not valid in its context” SocketException postgres 连接上的 System.Net.Sockets.SocketException - System.Net.Sockets.SocketException on postgres connection System.Net.Sockets.SocketException 灾难 - System.Net.Sockets.SocketException Disaster SocketException:请求的地址在上下文中无效 - SocketException: The requested address is not valid in the context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM