使用特定的本地端口通过TcpClient连接到远程计算机 [英] Connect to remote machine with TcpClient using a specific local port

查看:577
本文介绍了使用特定的本地端口通过TcpClient连接到远程计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
我正在使用TCP/IP上的套接字开发对等应用程序.
首先,我使用以下端口在端口2000上连接IP 72.55.168.241:

Hi!
I am developing a peer to peer application using sockets over TCP/IP.
First I connect to a IP 72.55.168.241 at port 2000 using:

tcpPeer.Connect(IPAddress.Parse("72.55.168.241"), 2000);


连接后,在端口4215上获得LocalEndPoint 59.99.51.255,在端口2000上获得RemoteEndPoint 72.55.168.241.

现在,我在端口3000上连接到另一个IP 115.117.148.142.


After the connection I get LocalEndPoint 59.99.51.255 at port 4215 and RemoteEndPoint 72.55.168.241 at port 2000.

Now i connect to the other IP 115.117.148.142 at port 3000.

tcpPeer.Connect(IPAddress.Parse("115.117.148.142"), 3000);


建立连接后,我希望端点地址如下所示:
端口4215上的LocalEndPoint 59.99.51.255和端口3000上的RemoteEndPoint 115.117.148.142.

我想说的是,每次我连接到每台远程计算机时,LocalEndPoint端口号都是相同的,例如使用特定端口(例如端口4215).


应用场景

请仔细阅读以下几行,尤其是第3点.我要实现这一点,这就是为什么我需要使用相同的端口.
假设客户端A希望与客户端B建立TCP连接,并且两者都位于NAT之后.像往常一样,我们假设A和B都已经与知名的集合服务器S建立了活动的TCP连接.该服务器记录每个注册客户端的公共和私有端点.


After the connection is established I want that the end point address will be like this:
LocalEndPoint 59.99.51.255 at port 4215 and RemoteEndPoint 115.117.148.142 at port 3000.

What I want to say is that every time when I connect to each of the remote machines my LocalEndPoint port number will be the same, e.g using a specific port such as port 4215.


Application Scenario

Please read the following lines carefully especially point No. 3. I want to implement this and that''s why I need to use the same port.
Suppose that client A wishes to set up a TCP connection with client B and both are behind the NAT. We assume as usual that both A and B already have active TCP connections with a well-known rendezvous server S. The server records each registered client''s public and private endpoints.


  1. 客户端A使用其与S的活动TCP会话来请求S帮助连接到B.
  2. S通过B的公共和私有TCP端点回复A,并同时发送A "到B的公共端点和私有端点.

  3. 从A和B用来向S注册的相同本地TCP端口中,A和B分别异步地进行到S所报告的到另一个公共和私有端点的传出连接尝试,同时侦听S上的传入连接.它们各自的本地TCP端口.

  4. A和B等待传出连接尝试成功和/或出现传入连接.如果由于网络错误(例如连接重置"或主机不可达")而导致传出连接尝试之一失败,则主机会在短暂的延迟(例如一秒钟)后重试该连接尝试,直至应用程序-定义最大超时时间.

  5. 建立TCP连接后,主机之间将相互验证以验证它们是否已连接到预期的主机.如果身份验证失败,则客户端将关闭该连接,并继续等待其他客户端成功.客户端使用此过程产生的第一个成功通过身份验证的TCP流.

  1. Client A uses its active TCP session with S to ask S for help connecting to B.
  2. S replies to A with B''s public and private TCP endpoints, and at the same time sends A''s public and private endpoints to B.

  3. From the same local TCP ports that A and B used to register with S, A and B each asynchronously make outgoing connection attempts to the other''s public and private endpoints as reported by S, while simultaneously listening for incoming connections on their respective local TCP ports.

  4. A and B wait for outgoing connection attempts to succeed, and/or for incoming connections to appear. If one of the outgoing connection attempts fails due to a network error such as "connection reset" or "host unreachable," the host simply re-tries that connection attempt after a short delay (e.g., one second), up to an application-defind maximum timeout period.

  5. When a TCP connection is made, the hosts authenticate each other to verify that they connected to the intended host. If authentication fails, the clients close that connection and continue waiting for others to succeed. The clients use the first successfully authenticated TCP stream resulting from this process.




请帮忙.




Please help.
Thanks in advance!

推荐答案

您可以通过使用TCPClient class''构造函数来做到这一点,该构造函数可让您指定本地终结点,如下所示:

You can do that by using the TCPClient class'' constructor that lets you specify a local endpoint like this:

//Example was taken from MSDN TCPClient Constructor with IEndPoint parameter
//Creates a TCPClient using a local end point.
IPAddress ipAddress = Dns.GetHostEntry (Dns.GetHostName ()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 0);
TcpClient tcpClientA = new TcpClient (ipLocalEndPoint);
//Later you can connect to the remote machine using
IEndPoint remoteEndpoint new IPEndPoint(yourRemoteIPAdress, yourRemotePort);
tcpClientA.Connect(remoteEndPoint);



我建议您尽管让系统通过选择可用端口来指定本地端点.如果您告诉我们为什么您声明此要求或什么情况导致您选择这种方式,这对您非常好.

最好的问候,
曼弗雷德(Manfred)



I would advise you though to let the system specify the local end point by choosing an available port. It would be very nice of you if you told us why you stated this requirement or what circumstances made you choose this way.

Best Regards,
Manfred


曼弗雷德(Manfred)提供了一个很好的答案,但还有更多需要考虑的事情.

Manfred provided an excellent answer, but there are a few more things to consider.

IPAddress ipAddress = Dns.GetHostEntry (Dns.GetHostName ()).AddressList[0];


可能会导致ipv6主机地址


May result in an ipv6 host address

IPEndPoint(ipAddress, 0);


正如曼弗雷德(Manfred)指出的那样,它将为您提供动态"分配的端口.您需要以与 FTP PORT [ ^ ]命令,用于ipv4端点,类似于 FTP EPRT [IPEndPoint(ipAddress, 0);
4.客户端在单独的线程或async
上侦听动态"分配的端口 5.客户端将端点信息发送到服务器(例如FTP PORT或FTP EPRT)
6.服务器使用提供的端点信息连接回客户端

问候
Espen Harlinn


will as Manfred points out give you a "dynamically" allocated port. You need to send this information to the other application in a manner simmilar to the FTP PORT[^] command for an ipv4 end point and like the FTP EPRT[^] command for ipv6. You are obviously free to implment your own command format; PORT and EPRT are just examples showing how it has been done before.

It goes like this:

1. Server listens on configured port
2. client connects
3. client allocates a port "dynamically" - IPEndPoint(ipAddress, 0);
4. client listens on "dynamically" allocated port on a separate thread, or async
5. client sends endpoint info to server (like FTP PORT or FTP EPRT)
6. server connects back to client using the provided endpoint info

Regards
Espen Harlinn


这篇关于使用特定的本地端口通过TcpClient连接到远程计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆