127.0.0.1
是运行应用程序的“这台计算机”或设备的内部地址。在现代网络中,每台计算机都将拥有 127.0.0.1 和至少 1 个其他 IP 地址。
要找出另一台 Windows 计算机的 IP 地址,您可以ipconfig
在命令提示符下使用。你会得到这样的东西:
Windows IP 配置
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . :
IP Address. . . . . . . . . . . . : 10.0.0.2
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 10.0.0.1
在这种情况下,10.0.0. 2是您可以用来从其他计算机连接到它的 IP 地址。像这样:
TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("10.0.0.2"), 8888);
client.Connect(serverEndPoint);
Windows 计算机也将有一个名称,例如JimsPC
或JimsPC.abc.com
,它也可以在TcpClient
构造函数中使用,或类似BeginConnect
的Connect
方法。
TcpClient client = new TcpClient("JimsPC", 8888);
或者
TcpClient client = new TcpClient();
client.Connect("JimsPC", 8888);