C# RemoteEndPoint 不存在

C# RemoteEndPoint doesnt exist

我正在用 C# 制作一个简单的 TCP 服务器。它有效,我可以连接到它,除了一件事,它说 RemoteEndpoint 不存在。我正在使用远程端点获取客户端 IP:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace tcpsockets
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener t = new TcpListener(IPAddress.Parse("0.0.0.0"),45123);
            t.Start();
            while (true)
            {
                Console.WriteLine("Waiting for a connection");
                TcpClient client = t.AcceptTcpClient();
                Console.WriteLine("Connection Recieved!");
                StreamReader sr = new StreamReader(client.GetStream());
                StreamWriter sw = new StreamWriter(client.GetStream());
                string text = sr.ReadLine();
                Console.WriteLine("text");
                IPEndPoint ipep = (IPEndPoint)t.RemoteEndpoint;
                IPAddress ipa = ipep.Address;
                Console.WriteLine(ipa.ToString());
                sw.WriteLine("Hello from the server");
                sw.Flush();
                client.Close();
            }
        }
    }
}

这是我的错误:

Error   1   'System.Net.Sockets.TcpListener' does not contain a definition for 'RemoteEndpoint' and no extension method 'RemoteEndpoint' accepting a first argument of type 'System.Net.Sockets.TcpListener' could be found (are you missing a using directive or an assembly reference?)   c:\users\logan\documents\visual studio 2013\Projects\tcpsockets\tcpsockets\Program.cs   27  49  tcpsockets

TcpListener确实没有RemoteEndPoint。您可能需要 client.Client.RemoteEndPoint

实际上 TCP 侦听器的目的非常简单,仅限于接受新的客户端连接。一旦客户端被接受(建立新连接),侦听器就会忘记刚刚到达。所有进一步的通信都通过 TcpClient 实例(以及底层 Socket - TcpClient.Client Property)。因此,您不应期望侦听器知道任何远程端点。