问题描述
我正在尝试创建一个同时使用 TCP 和 UDP 的服务器和客户端.服务器在 LAN 设置中运行良好,但在通过 WAN 传输时未接收到 UDP 消息.我相信这是因为用于发送数据的 UDP 套接字没有在 NAT 表中保留足够长的时间来返回任何信息.有没有办法让 UDP 端口在路由器中保持打开(没有端口转发)或使用与已连接的 TCP 连接相同的 UDP 端口?提前致谢.
I am attempting to create a server and client that utilizes both TCP and UDP. The server works very well in a LAN setting but the UDP messages are not being received when transmitted over a WAN. I believe it is because the UDP socket used to send the data is not remaining in the NAT tables long enough to return any information. Is there a way to either make the UDP port stay open in the router (without port forwarding) or use the same port for UDP as the already connected TCP connection? Thanks in advance.
推荐答案
如果您没有获得任何流量,则可能只是被防火墙阻止了.在这种情况下,它不是关于转发,而是关于打开端口.
If you're not getting any traffic it is probably simply blocked by the firewall. In this case it is not about forwarding, it is about opening the port.
大多数(如果不是全部)NAT/防火墙设备将允许双向 UDP 流量,一旦通过 NAT 打孔.也就是说,如果我的笔记本电脑位于 NAT/防火墙后面,将 UDP 数据包发送到 Internet,我的 NAT/防火墙将允许将 UDP 流量返回到原始端口号.我经常使用 UDP,我的经验是这是规则,很少有例外.
Most (if not all) NAT/Firewall devices will allow UDP traffic in both directions once a hole is punched through the NAT. That is, if my laptop here, sitting behind a NAT/firewall, sends a UDP packet out to the Internet my NAT/firewall will allow return UDP traffic to the originating port number through. I work a lot with UDP and my experience is that this is the rule and very few exceptions.
请记住,虽然 UDP 数据包不能保证被传送.
Keep in mind though UDP packets are not guaranteed to be delivered.
您的客户端是否在 NAT 后面?客户端发送的任何数据包都会到达服务器吗?是服务器到客户端方向的问题吗?
Is your client behind a NAT? Do any packets the client send get to the server? Is the problem in the server to client direction?
如果您对 UDP 和 TCP 使用相同的端口号,这不会改变这种情况.您不能搭载 TCP 连接,因为它是不同的协议.
If you use the same port number for UDP and TCP this will not change the situation. You can't piggyback on a TCP connection because it is a different protocol.
单播 UDP 的网络地址转换 (NAT) 行为要求
http://en.wikipedia.org/wiki/UDP_hole_punching
这篇关于WAN 上的 TCP 和 UDP 套接字服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!