我正在使用 TcpClient 和套接字开发 P2P 聊天应用程序。
我编写了以下代码来接受 tcpclient:
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
IPAddress ip_local = Dns.GetHostAddresses(Dns.GetHostName())[0];
// IPAddress ip_local = IPAddress.Parse(ip_local);
TcpListener tcpl = new TcpListener(new IPEndPoint(ip_local, 9277));
while (true)
{
try
{
tcpl.Start();
TcpClient tcpClient = tcpl.AcceptTcpClient();
StateObject state = new StateObject();
state.workSocket = tcpClient.Client;
tcpClient.Client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(OnReceive), state);
}
catch (Exception ex)
{
}
}
问题是它每次都选择不同的网络[因为我有 1 个 LAN 和 2 个 VMWARE 网络]。那么问题是如何强制它获取局域网的网络地址,即特定的网络?