问题描述
这是问题,这很简单(理解......):
Here's the problem, it's very simple (to understand..):
我家里有2台电脑,它们都有相同的公共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重载,但通常称为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
直接到计算机A的 9000
端口:
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.
希望这是有道理的。
这篇关于当网络上的所有计算机具有相同的公共IP地址时,如何将UDP数据包发送到特定计算机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!