RemoteEndPoint vs. LocalEndPoint

本文关键字:LocalEndPoint vs RemoteEndPoint | 更新日期: 2023-09-27 18:28:36

作为C#网络的初学者,我编写了一个简单的Client-Server应用程序。我正在连接到我的本地IPAddress和服务器正在侦听的端口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之间有什么区别呢?LocalEndpointPort(在我的示例中为46715)有什么用途?它来自哪里?谢谢

RemoteEndPoint vs. LocalEndPoint

远程端点将显示哪个客户端(ip)连接到您的本地端点(更有可能是服务器ip 127.0.0.1)