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

烈酒焚心 提交于 2020-01-04 03:01:30

问题


I have a server with public Ip Address and a client machine behind NAT which are implemented in C# language. I've tried to program a tcp hole punching but got error when made socket from server to client. 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

2) 2nd, Server got the msg and parse it

3) 3rd, I got the remoteEndPoint IpAddress & port number ( =Nat router Ip & Port ) on the server side

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

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 X.X.X.X: 24352"

Could anyone help me how to solve the problem.

Thanks in advance.


回答1:


You problem is probably here:

and Hope the router send my msg to client behind nat

Nat traversal (such as hole punching) depends on the implementation of the specific 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. NOT ALL NATs DO THAT!

From Microsoft Technet:

The following types of NATs are defined:

  • 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. 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.

  • 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. 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.

  • 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).

Your hole punching approach will work for Cone and address restricted NATs. Not for port restricted NATs of symmetric NATs (which is probably the NAT in your case).



来源:https://stackoverflow.com/questions/25083683/got-error-when-implementing-c-sharp-tcp-hole-punching-cannot-connect-behind-na

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!