互联网上的UDP.它是如何工作的? [英] UDP over Internet. How does it work?

查看:116
本文介绍了互联网上的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是否有意义?还是我需要播放器激活Portforwarding并打开Port等?

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.

就我的编程观点而言,我希望有一种直观使用它的方法. 像创建DatagramSocketDatagramPacket一样,使用数据和目标配置数据包,然后通过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.

对于我的用户,我不希望他们必须配置任何特定的东西,例如打开他们要使用的确切端口等.我只希望他们使用该程序(服务器和客户端),它应该可以工作. /p>

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

这篇关于互联网上的UDP.它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆