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
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
–
–
–
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();
–
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.
–
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.