简体   繁体   English

实现C#Tcp打孔时出错 - 无法连接NAT

[英]Got Error when Implementing C# Tcp Hole Punching - Cannot Connect Behind NAT

I have a server with public Ip Address and a client machine behind NAT which are implemented in C# language. 我有一个公共IP地址的服务器和NAT后面的客户端机器,用C#语言实现。 I've tried to program a tcp hole punching but got error when made socket from server to client. 我试图编写一个tcp打孔,但是当从服务器到客户端进行套接字时出错。 My steps are as below: 我的步骤如下:

1) 1st, I make a socket connection to Public Server and send msg to it and started listening on port 17000 1)1,我与公共服务器建立套接字连接并向其发送消息并开始侦听端口17000

2) 2nd, Server got the msg and parse it 2)2,Server获取了msg并解析它

3) 3rd, I got the remoteEndPoint IpAddress & port number ( =Nat router Ip & Port ) on the server side 3)3,我在服务器端获得了remoteEndPoint IpAddress和端口号(= Nat router Ip&Port)

4) 4th try to connect to socket ( NAT Ip,Nat Port ) and send msg to client on port 17000 from server => means= I've made socket to nat router and Hope the router send my msg to client behind nat 4)第4次尝试连接到套接字(NAT Ip,Nat端口)并从服务器端口17000向客户端发送消息=>意味着=我已经将套接字发送到nat路由器并希望路由器将我的消息发送到nat后面的客户端

But I've got the following error: "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond XXXX: 24352" 但是我遇到以下错误:“连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机未能响应XXXX:24352”

Could anyone help me how to solve the problem. 任何人都可以帮我解决问题。

Thanks in advance. 提前致谢。

You problem is probably here: 你的问题可能在这里:

and Hope the router send my msg to client behind nat 并希望路由器将我的消息发送给nat后面的客户端

Nat traversal (such as hole punching) depends on the implementation of the specific NAT. Nat遍历(如打孔)取决于具体NAT的实现。 You assume that since the NAT saw a packet from the client's port 17000, and mapped it to some NAT port X, then it will forward all packets from port X back to the client. 您假设由于NAT从客户端端口17000看到一个数据包,并将其映射到某个NAT端口X,然后它会将端口X中的所有数据包转发回客户端。 NOT ALL NATs DO THAT! 不是所有NAT都这样做!

From Microsoft Technet: 来自Microsoft Technet:

The following types of NATs are defined: 定义了以下类型的NAT:

  • Cone NATs: A NAT in which the NAT translation table entry stores a mapping between an internal address and port number and an external address and port number. 锥形NAT:NAT转换表条目存储内部地址和端口号与外部地址和端口号之间的映射的NAT。 Once the NAT translation table entry is in place, inbound traffic to the external address and port number from any source address and port number is allowed and translated. 一旦NAT转换表条目到位,就允许并转换来自任何源地址和端口号的外部地址和端口号的入站流量。

  • Restricted NATs: A NAT in which the NAT translation table entry stores a mapping between an internal address and port number and an external address and port number, for either specific source addresses or specific source address and port numbers. 受限制的NAT:NAT转换表条目存储内部地址和端口号与外部地址和端口号之间的映射的NAT,用于特定源地址或特定源地址和端口号。 An inbound packet that matches the NAT translation table entry for the external destination address and port number from an unknown external address or port number is silently discarded. 从未知外部地址或端口号匹配外部目标地址和端口号的NAT转换表条目的入站数据包将被静默丢弃。

  • Symmetric NATs: A NAT that maps the same internal address and port number to different external addresses and ports, depending on the external destination address (for outbound traffic). 对称NAT:将相同的内部地址和端口号映射到不同的外部地址和端口的NAT,具体取决于外部目标地址(对于出站流量)。

Your hole punching approach will work for Cone and address restricted NATs. 您的打孔方法适用于Cone并解决受限制的NAT问题。 Not for port restricted NATs of symmetric NATs (which is probably the NAT in your case). 不适用于对称NAT的端口受限NAT(在您的情况下可能是NAT)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM