use*_*486 6 c# sockets endpoints
作为 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);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
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
Run Code Online (Sandbox Code Playgroud)
那么 RemoteEndPoint 和 LocalEndPoint 有什么区别呢?LocalEndPoint 端口(在我的示例中为 46715)的用途是什么?它来自哪里?谢谢。