I am using TcpListener class to create a TCP socket on a certain IP and port in my machine. The code I use is as follows :
tcpListener = new TcpListener("192.168.0.110", 8005);
try
{
tcpListener.Start();
}
catch (Exception e)
{
Console.WriteLine(e);
return;
}
This works fine. I want to use a check thread to check the status of the listener.
private void CheckConnections()
{
while (true)
{
if (_tcpListener.Server.IsBound)
{
Console.WriteLine(_tcpListener.Active);
Console.WriteLine("IsBound : True");
}
else
{
Console.WriteLine(_tcpListener.Active);
Console.WriteLine("IsBound : False");
}
Thread.Sleep(2000);
}
}
When I change the IP of my machine manually from adapter settings, for example from "192.168.0.110" to "192.168.0.105", in the "netstat -ano" command I still see that the " 192.168.0.110:8005" is in Listening state. Also I could not find a way to subscribe to this change from my code. So my question is how to handle IP change on server side socket? Is there a way that I can get IP change information from the socket itself?