如何用TcpListener同时监听多个网卡的IP地址
其实很简单,把特定的IPAddress改成 IPAddress.any 就可以了.
IPAddress ipAddr = IPAddress.Parse(ip);
if (ip == "0.0.0.0")
{
ipAddr = IPAddress.Any;
}
IPEndPoint localEP = new IPEndPoint(ipAddr, port);
if (tcpListener != null)
{
tcpListener.Stop();
}
tcpListener = new TcpListener(localEP);
Thread t = new Thread(new ParameterizedThreadStart(this.ThreadListen));
t.Start(tcpListener);
if (ip == "0.0.0.0")
{
ipAddr = IPAddress.Any;
}
IPEndPoint localEP = new IPEndPoint(ipAddr, port);
if (tcpListener != null)
{
tcpListener.Stop();
}
tcpListener = new TcpListener(localEP);
Thread t = new Thread(new ParameterizedThreadStart(this.ThreadListen));
t.Start(tcpListener);
QQ:273352165
evlon#126.com
转载请注明出处。