通过 Internet 的 UDP.它是如何工作的?
当我正在编写网络聊天(java,但不应该对问题产生影响)并想使用 UDP 时,我遇到了它无法通过 Internet 工作的问题.经过一番研究,我发现您必须激活特定端口的端口转发.所以现在我的问题来了:
As I am programming a network chat (java, but should not make a difference for the question), and wanted to use UDP, I ran into the problem of it not working over the internet. After a little research I found out that you have to have port forwarding for the specific port activated. So now it comes to my question:
UDP 是否以不可配置的方式在 Internet 上工作?
Does UDP work over the Internet in a not configurable way?
例如,如果我要编写整个网络游戏,那么使用 UDP 是否有意义?或者我会要求播放器激活端口转发并打开端口等?
For example, if I would program a whole Network Game would it make sense to use UDP? Or would I require the Player to activate Portforwarding and open the Port etc?
那么什么时候使用 UDP 才有意义?为什么?
When would it make sense to use UDP then? And why?
那时我实际上并不了解 UDP 的全部意义.
I'm actually not understanding the whole point of UDP then.
就我的编程观点而言,我希望有一种方法可以直观地使用它.与创建 DatagramSocket
和 DatagramPacket
一样,使用 Data 和 Destination 配置 Packet,然后通过 Internet 将其发送出去.
For my programming point of view I would like to have a way to use it intuitive.
Like creating the DatagramSocket
and the DatagramPacket
, configure the Packet with the Data and the Destination and send it away over the internet.
对于我的用户,我不希望他们必须配置任何特定的东西,例如打开他们想要使用的确切端口等.我只希望他们使用该程序(服务器和客户端)并且它应该可以工作.
As for my Users I don't want them to have to configure any specific things like opening the exact port they want to use etc. I just want them to use the program (server and client) and it should work.
推荐答案
您遇到的问题不是 UDP 与 TCP 之一(尽管使用不可靠、无序的 UDP 作为聊天应用程序的基础似乎很奇怪选择给我).
The problem you've run into is not one of UDP vs TCP (although using the unreliable, unordered UDP as the basis of a chat application seems like an odd choice to me).
问题在于 NAT 遍历.简而言之,家庭路由器执行称为 NAT - 网络地址转换的网络功能.他们这样做是为了对 NAT 内的所有机器使用一个公共 IP 地址(给定私有地址 - 通常是 10.0.0.0 或 192.168.0.0).然后,路由器将从 LAN 内部发送的所有数据包中的源 IP 地址从私有地址切换到公共地址.它使用端口号记住"哪台机器将什么发送到哪个地址,以便在响应到达时执行反向转换.
The problem is that of NAT traversal. In a nutshell, home routers perform a network function called NAT - Network Address Translation. They do it in order to use a single public IP address for all machines inside the NAT (which are given private addresses - usually 10.0.0.0 or 192.168.0.0). The router then switches the source IP address in all packets sent from inside the LAN from the private address to the public one. It uses port numbers to "remember" which machine sent what to what address, in order to perform the backwards translation when the response arrives.
当有人想要启动到 NAT 后面的机器的连接时,就会出现问题.在没有先看到传出连接的情况下,NAT 不知道它应该将数据包转发到哪个内部计算机和端口.这就是发生在你身上的事情.
The problem arises when someone wants to initiate a connection to a machine behind a NAT. Without seeing an outgoing connection first, the NAT doesn't know to which internal computer and port it should forward the packet. This is what happens to you.
这个问题有多种解决方法,最简单的一种是手动端口转发(正如您所发现的),但这是任何对等应用程序都面临的众所周知的问题.如果您需要联系 NAT 后面的机器(即联系大多数家庭用户)并且您希望您的应用程序开箱即用(无需您的用户摆弄他们的路由器),您需要研究 NAT 穿越技术,在您的应用,希望用户家的路由器支持.这是一个巨大的颈部疼痛.
There are various fixes for this issue, with the simplest one being manual port forwarding (as you've discovered), but it's a well known problem faced by any peer-to-peer application. If you need to contact a machine behind NAT (i.e. contact most home users) and you want your application to work out-of-the box (without your users fiddling with their routers) you need to research NAT traversal techniques, implement them in your application, and hope that the user's home routers support them. It's a huge pain in the neck.
相关文章