当网络上的所有计算机具有相同的公共 IP 地址时,如何将 UDP 数据包发送到特定计算机?
问题来了,很简单(好理解..):
Here's the problem, it's very simple (to understand..):
我家里有两台电脑,它们都有相同的公共 IP 地址(例如 1.2.3.4).
I have 2 computers at home, they both have the same public IP address (e.g. 1.2.3.4).
我在咖啡店有一台电脑(不同的网络),所以它有不同的公共 IP 地址.
I have 1 computer at a coffee place (different network) so it has a different public IP address.
我想从咖啡店的电脑向我家中的一台电脑发送一条消息(例如hi").
I want to send a message (e.g. "hi") from the computer at the coffee place to ONE of computers I have at home.
我正在使用Java,为发送者考虑以下非常简单的程序(为简单起见,我去掉了异常处理):
I'm using Java, think of the following very simple program for the sender (I took off exception handling for simplicity):
主要是我做的:
sendPacket("hi");
我有
void sendPacket(String message){
DatagramSocket myServerSocket = new DatagramSocket(9000); // server socket
byte[] sendData = new byte[message.length()]; // build msg
sendData = message.getBytes();
InetSocketAddress destSocketAddr = new InetSocketAddress("1.2.3.4", 9000); // destination socket addr
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, destSocketAddr); // make packet
myServerSocket.send(sendPacket); // send packet
}
如果我在家中的两台计算机上运行我的侦听器(接收器)(都具有相同的公共 IP 地址 1.2.3.4),我如何指定我打算将此消息发送到哪一台?
If I have my listener (receiver) running on both computers at home (both with the same public IP address 1.2.3.4) how can I specify which one I intend to send this message to?
推荐答案
如果你家的两台电脑有相同的公共 IP 地址,这意味着这些电脑正在使用 NAT 或 网络地址转换(严格来说是端口地址转换或NAT Overload,但通常简称为NAT).
If both of your home computers have the same public IP address, that means those computers are using NAT or Network Address Translation (strictly speaking, it's Port Address Translation or NAT Overload, but commonly referred to as just NAT).
这意味着,为了启动从外部到 NAT 内任何机器的连接,必须在您的路由器(通常是调制解调器)中设置 端口转发,以便您将您的公共家庭 IP 地址的特定端口映射到您家中的特定私有 IP 地址.
What this means is that in order to initiate a connection from the outside to any of your machines inside NAT, a Port Forwarding must be set in your router (typically your modem), so that you map a specific port of your public home IP address to a specific private IP address inside your home.
假设您家中有这样的计算机 A 和 B:
Let's say you have computers A and B in your home like this:
Router / Modem
192.168.0.1
||
++=========++========++
|| ||
Computer A Computer B
192.168.0.2 192.168.0.3
现在,假设您需要计算机 A 监听 TCP 端口 9000
(端口可以主要是 TCP 或 UDP),您可以将公共端口 9000
直接转发到计算机 A9000
端口:
Now, let's assume you need Computer A listening on TCP port 9000
(ports can mainly be TCP or UDP), you could forward public port 9000
directly to Computer A's 9000
port:
Forward TCP/UDP on public port 9000 to private port 9000 on 192.168.0.2
要向计算机 A 发送消息,只需将其发送到 1.2.3.4:9000
.但是如果另一台 PC 也只监听端口 9000
怎么办?您也不能分配公共端口 9000
,因为它是由计算机 A 占用的.您可以这样做:
To send a message to computer A, just send it to 1.2.3.4:9000
. But what if the other PC only listens on port 9000
too? You cannot also assign public port 9000
because it is taken by Computer A. You could do this:
Forward TCP/UDP on public port 9001 to private port 9000 on 192.168.0.3
这样,计算机 B 仍然会在端口 9000
上接收消息,但它们需要通过 Internet 发送到 1.2.3.4:9001
.当数据包进入(和离开!)您的家庭网络时,您的路由器的 NAT 会自动转换端口.
This way, computer B still receives messages on port 9000
, but they would need to be sent across the Internet to 1.2.3.4:9001
. Your router's NAT automatically translates the port as the data packets enter (and leave!) your home network.
最后,发送者需要调整目标端口,以便与 NAT 后面的不同机器对话".
In the end, the sender would need to adjust the destination port in order to 'talk' to different machines behind NAT.
希望这是有道理的.
相关文章