问题描述
我有一个带有公共IP地址的服务器和一台以C#语言实现的位于NAT后面的客户端计算机.我尝试对tcp打孔进行编程,但是在从服务器到客户端建立套接字时出现错误.我的步骤如下:
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)首先,我建立了与Public Server的套接字连接,并将消息发送给它,并开始在端口17000上侦听
1) 1st, I make a socket connection to Public Server and send msg to it and started listening on port 17000
2)第二,服务器获取了消息并进行了解析
2) 2nd, Server got the msg and parse it
3)第三,我获得了remoteEndPoint IpAddress&服务器端的端口号(= Nat路由器IP和端口)
3) 3rd, I got the remoteEndPoint IpAddress & port number ( =Nat router Ip & Port ) on the server side
4)第4次尝试连接到套接字(NAT Ip,Nat Port)并从服务器将msg发送到端口17000上的客户端=>意味着=我已经将套接字设置为nat路由器,希望路由器将我的msg发送给后面的客户端纳特
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
但是我遇到了以下错误:连接尝试失败,因为连接的用户在一段时间后未正确响应,或者连接建立失败,因为连接的主机未能响应X.X.X.X:24352"
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"
谁能帮助我解决问题.
谢谢.
推荐答案
您的问题可能在这里:
Nat遍历(例如打孔)取决于特定NAT的实现.您假设由于NAT从客户端的端口17000看到了一个数据包,并将其映射到某个NAT端口X,那么它将把所有来自端口X的数据包转发回客户端. 并非所有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!
-
锥体NAT:一种NAT,其中NAT转换表条目存储内部地址和端口号与外部地址之间的映射 地址和端口号.进入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. 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:一种NAT,其中NAT转换表条目存储内部地址和端口号与一个内部地址之间的映射. 外部地址和端口号,用于特定的源地址 或特定的源地址和端口号.入站数据包 与外部目标的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. 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:一种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的端口受限NAT(在您的情况下可能是NAT).
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).
这篇关于实施C#Tcp打孔时出现错误-无法在NAT之后进行连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!