简体   繁体   中英

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. 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.

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.

Now we deployed this on another prod server by passing IPAddress of that server on which it is deployed, Here It is throwing error:

SocketException: {0}System.Net.Sockets.SocketException (0x80004005): The requested address is not valid in its context.

This error always comes on 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. It worked fine, How my concern is why IPAddress is not working fine.

  2. I checked Port binding by "netstat -nao|findstr 700", This port is not binded.

  3. I checked system ip by ipconfig it is same as passed in IPAddress.

  4. I checked ping by ping 10.83.77.254, ping is working fine.

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

I don't know why it won't work with the exact IP address of the server. 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. You cannot bind to an interface that is not connected.

Also you should convert this to fully async code.

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. As on starting Service gets started immediately, however network connection in turn IP Assignment takes time. Now I set Startup Type Automatic (Delayed Start) and It is working fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM