提问人:Dominique 提问时间:11/6/2023 最后编辑:Dominique 更新时间:11/6/2023 访问量:69
为什么我无法将 TCP 套接字绑定到另一个 IP 地址?
Why can't I bind a TCP socket to another IP address?
问:
在客户的站点上,以下代码就像一个魅力一样工作:
private IPEndPoint GetCurrentServerIpAddress()
{
...
return new IPEndPoint(IPAddress.Parse("192.168.13.9"), 12714);
}
var IPAddress = GetCurrentServerIpAddress();
_socket.Bind(IPAddress);
由于我需要解决该客户网站上的错误,因此我正在尝试在自己的PC上启动该程序。为此,我想使用另一台 IP 地址为“10.0.1.60”的测试计算机(无论是虚拟机),我想在其中做同样的事情(但显然使用新的 IP 地址:
private IPEndPoint GetCurrentServerIpAddress()
{
...
return new IPEndPoint(IPAddress.Parse("10.1.0.60"), 12714);
}
var IPAddress = GetCurrentServerIpAddress();
_socket.Bind(IPAddress);
然而,这是行不通的:我总是陷入 ,提到“请求的地址在其上下文中无效”。System.Net.Sockets.SocketException
我清楚地看到“192.168.xxx.yyy”和“10.0.1.zzz”是不同的(种类)地址,这可能会导致此问题。
我该怎么做才能避免该错误消息并能够从我的测试计算机发送 TCP 消息?
编辑:我的 PC
的“ipconfig” 最后一条评论提到了我计算机(即 Windows-10 计算机)界面的条件。由于我不知道如何解释此注释,因此命令结果如下:ipconfig
Prompt>ipconfig
Windows IP Configuration
Ethernet adapter Ethernet 4:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Ethernet 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . : company.int
Link-local IPv6 Address . . . . . : fe80::4c66:9ac5:dbd0:656%25
IPv4 Address. . . . . . . . . . . : 10.1.3.19
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 10.1.3.254
Wireless LAN adapter Wi-Fi:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 1:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Local Area Connection* 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter VMware Network Adapter VMnet1:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe60::b53a:f868:31d0:e5a9%5
IPv4 Address. . . . . . . . . . . : 192.168.133.11
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Ethernet adapter VMware Network Adapter VMnet8:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe60::332e:9c68:4e2d:ceb7%27
IPv4 Address. . . . . . . . . . . : 192.168.42.11
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Ethernet adapter Ethernet 3:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Talk2m-eCatcher:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Ethernet 9:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Ethernet adapter Ethernet 10:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
答: 暂无答案
评论
addressFamily
Internetwork