相关文章推荐
文雅的登山鞋  ·  linux下把 python ...·  3 月前    · 
拉风的猴子  ·  Types.Nvarchar Field ...·  7 月前    · 
大鼻子的企鹅  ·  FluxCD 实现 kubernetes ...·  1 年前    · 
鬼畜的椰子  ·  php ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

I developed a TCP Listener. Below is main coding of listener:

public static void GetXMLStream()
       TcpListener lisetner = null;
            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:

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

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

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

  • 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

    look like a socket exception just check if there are any HTTP client calls happening in the application. – Rajeesh Menoth Aug 20, 2022 at 5:26 @RajeeshMenoth, No http client call is happening, also your mentioned question does not help. As I am not looking to handle this exception, I am looking for it’s root cause. For me it’s looking like server ip config error, still I am not able to solve it. – Ravi Kumar Aug 20, 2022 at 8:00 I bet its environmental. OS, Windows updates (is it Windows?), firewall, security policies, NIC settings, ipv6 settings (could steal your upv4 connections) etc.. Not an SO question - if it works everywhere ex one machine. I'd disable ipv6 on all nics first see if it helps – Boppity Bop Aug 21, 2022 at 8:53

    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;
            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();
                    Yes, I completely agree with your code enhancement, But currently my problem was only above mentioned problem.
    – Ravi Kumar
                    Aug 24, 2022 at 10:18
    

    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.

    You should probably have some sort of retry logic within the service, in case there is a socket exception or the network is disconnected etc. – Charlieface Aug 24, 2022 at 17:30

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •