How to send a UDP packet to a specific computer when the entire computer on the network has the same public IP address?

Here's the problem, it's very simple (understand ..):

I have 2 computers at home, they both have the same public IP address (e.g. 1.2.3.4).

I have a computer in a coffee place (on another network), so it has a different public IP address.

I want to send a message (for example, "hello") from a computer in a coffee place to ONE of the computers that I have at home.

I use Java, I think of the following very simple program for the sender (I just used exception handling):

I basically do:

sendPacket("hi"); 

and I

 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 } 

If I have a listener (receiver) running on both computers at home (both with the same public IP address 1.2.3.4), how can I indicate which one I intend to send this message to?

+6
source share
2 answers

If both of your home computers have the same public IP address, this means that these computers use NAT or Network Address Translation (strictly speaking, this is Port Address Translation or NAT Overload, but usually called just NAT).

This means that in order to initiate an external connection to any of your machines inside NAT, you must specify Port Forwarding on your router (usually your modem) so that you map a specific port on your public IP address to the IP address of your private IP addresses inside your home.

Let's say you have computers A and B in your house, like this:

  Router / Modem 192.168.0.1 || ++=========++========++ || || Computer A Computer B 192.168.0.2 192.168.0.3 

Now suppose you need computer A listening on TCP port 9000 (the ports can basically be TCP or UDP), you can redirect open port 9000 directly to port A 9000 :

 Forward TCP/UDP on public port 9000 to private port 9000 on 192.168.0.2 

To send a message to computer A, just send it to 1.2.3.4:9000 . But what if the other computer is only listening on port 9000 too? You also cannot assign the public port 9000 , because it is taken by computer A. You can do this:

 Forward TCP/UDP on public port 9001 to private port 9000 on 192.168.0.3 

Thus, computer B still receives messages on port 9000 , but they will need to be sent over the Internet at 1.2.3.4:9001 . Your NAT router automatically forwards the port as data packets enter (and leave!) Your home network.

In the end, the sender will need to configure the destination port to "talk" with different machines for NAT.

Hope this makes sense.

+12
source

Typically, these NAT firewalls will redirect traffic back to the source computer for you.

So, if you had one machine sending traffic to your coffeeshop machine on port 5000 , and another sending traffic to your coffeeshop machine on port 5001 , the router will keep track of which port is intended for which client, So when you send packets back from port 5000 , it will go to the first machine, and when you send packets back from port 5001 , it will go to the second machine.

The unfortunate part is that your machine at coffeeshop is probably also located behind the NAT firewall, and your home machines may also not be able to access it directly.

If you can host the server on a good network, then both partners can contact the server and transfer all traffic through it. This is a good option, but it does not scale well. (For three cars, this is not a big deal. For three million cars, this is a big deal.)

You can examine other parameters to try to cross the NAT firewall , such as UPnP , but these mechanisms usually require a certain way for clients to negotiate sessions before they work.

+3
source

Source: https://habr.com/ru/post/913895/


All Articles