作为 C# 网络的初学者,我编写了一个简单的客户端-服务器应用程序。我正在连接到服务器正在监听的本地 IP 地址和端口 8080。
在客户端:
IPAddress remoteaddr = IPAddress.Parse("127.0.0.1");
int port = 8880;
TcpClient tcpclient = new TcpClient();
tcpclient.Connect(remoteaddr, port);
NetworkStream networkstream = tcpclient.GetStream();
IPEndPoint RemAddr = (IPEndPoint)tcpclient.Client.RemoteEndPoint;
IPEndPoint LocAddr = (IPEndPoint)tcpclient.Client.LocalEndPoint;
if (RemAddr != null)
{
// Using the RemoteEndPoint property.
Console.WriteLine("I am connected to " + RemAddr.Address + "on port number " + RemAddr.Port);
}
if (LocAddr != null)
{
// Using the LocalEndPoint property.
Console.WriteLine("My local IpAddress is :" + LocAddr.Address + "I am connected on port number " + LocAddr.Port);
}
输出是:
I am connected to 127.0.0.1 on port number 8880
My local IpAddress is :127.0.0.1 I am connected on port number 46715
那么 RemoteEndPoint 和 LocalEndPoint 有什么区别呢? LocalEndPoint 端口(在我的示例中为 46715)有什么用,它来自哪里? 谢谢。
最佳答案
远程端点将向您显示哪个客户端 (ip) 连接到您的本地端点(更有可能是服务器 ip 127.0.0.1)
关于c# - RemoteEndPoint 与 LocalEndPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34558328/