Help communicating with sockets via nat

I want to make a simple p2p messenger in java, I was able to contact 2 computers on the same network, but when I try to send a message to another computer from outside the network, the message disappears ... I know that I could make transitions to router, but I don’t want to do this, I want to make it look like yahoo, gtalk or other im servers. I think they use the nat system for this, but I do not know very well how: D.

Can someone explain to me how NAT works, and if this is the way to solve my problem?

I read that I need a static and public ip for the server, and when the client (with closed ip) sends a request to the server, its NAT will know what to do with the message that will be from the server next time ... but if the client does not send packet to the server, and the server sends the packet to the client, then NAT does not know where to send the packet within the network (to the client).

Is what I think right?

Many thanks!

+3
source share
3 answers

You are almost right in your description of NAT. The only mistake you made was what Peter pointed out - the client is always the initiator of the connection.

Now, if both sides have public IP addresses, everything is fine, and both sides can be a server.

NAT, IP-, , , NAT . NAT- , .

, , NAT, , , . , " ". , , , Skype, .

+5

, , . , , . BTW: , .

; , , , .

0

, , IP-, . , . , , , :

  • IP-, . .
  • , IP-.

Your system may work inside the local network, but it will not work on the Internet. Each computer on the network has a local IP assigned to it by the local gateway. This local IP address is not visible to anyone unless the NAT parameter displays a public IP address. There should also be a setting for outgoing messages.

0
source

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


All Articles