c# - RemoteEndPoint 与 LocalEndPoint

标签 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);
        }

输出是:

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/

相关文章:

c# - .NET MAUI - Controller 句柄地址

php - 如何使用 PHP 和 C 将大图保存在内存中?

c - 获取传入套接字连接的源地址

r - 使用 R 的 xts 中的端点

带有过滤和授权的 RESTFul API 端点设计

C# - 将类的新实例添加到数组

c# - 如何在 C# 中将一个集合划分为每个 50 个元素的子集合?

C#-如何在转换中处理字符串终止符

java - 通过套接字发送压缩数据

instagram - 如何从 Instagram 中的媒体检索评论?