RemoteEndPoint与LocalEndPoint

时间:2016-01-01 17:28:01

标签: c# sockets endpoints

作为C#网络的初学者,我编写了一个简单的客户端 - 服务器应用程序。我正在连接到我的本地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有什么区别? LocalEndPoint端口有什么用(在我的例子中是46715),它来自哪里? 感谢。

1 个答案:

答案 0 :(得分:1)

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