What happens when 2 computers listen on the same port and the router receives a packet through this port

What I ask is that two computers are listening on the same port, and a packet of information arrives at the router through the WAN Ip and the same port. Will the package go to both computers? None? One or the other?

IE

computer 1 - (internal IP) β†’ 192.168.1.3 - (listens on the port) β†’ 4444

computer 2 - (internal IP) β†’ 192.168.1.2 - (listens on the port) β†’ 4444

computer 3 - (connects and sends) β†’ 24.157.358.45-00-00444

package β†’ computer 1 and computer 2

Code in VB6:

LAN.LocalPort = 4444 LAN.Protocol = sckTCPProtocol LAN.Listen 

I am using a WinSock object in Microsoft WinSock Control 6.0 in VB6 Professional

If there is something to clarify, I would be more than happy.

+4
source share
4 answers

From my knowledge of routers, if a port is not configured, the router will discard any packets sent on that port.

If port forwarding is configured, only one of the computers can be configured to receive packets.

+4
source

The router does not send an incoming packet to any machine if communication is not already established.

If 192.168.1.3 calls some other computer (for example, 4.5.6.7) from its port 4444, the router will assign an arbitrary port to its external address (say, 24.157.358.45 [sic]: 5555) and will transmit packets to 4.5.6.7. 4.5.6.7 will send response packets to 24.157.358.45DUC555 - since this is the only address that he knows about, and the router will transmit them 192.168.1.3-00-00444.

This is the normal course of things, but there are many additional details to this scheme that allow you to establish communication with the machine behind the router through a hoax.

The system of having machines with private IP addresses behind a router with a common address is called Network Address Translation (NAT) ; This is a pretty deep topic.

+2
source

If the packet is an incoming request to establish a new TCP connection with the server that runs behind the router, the router must have an explicit port forwarding rule configured either statically in the router configuration or dynamically through uPNP or SNMP, which tells the router where to forward incoming packets via 24.157.358.45:4444, up to 192.168.1.2-00-00444 or up to 192.168.1.3-00-00444, otherwise the packet will be discarded. So no, both of your listening servers will not see the same packet.

Once the TCP connection is established, the router will know which specific LAN machines are associated with these connections, and accordingly will forward incoming packets belonging to these connections.

+1
source

The previous answers are correct, you need to enable port forwarding. If it is not enabled, port 4444 will be closed on the router.

It looks like you have multiple servers and want to redirect to any server that is currently on. This is not possible (*), the router doesn’t care if PC1 or PC2 listens on port 4444, it just redirects everything to the address configured when forwarding the ports.

(*): Good, but it takes some extra work.

Solution 1: Trick the router into thinking that there is only one server. Give PC1 and PC2 a virtual network interface with the same IP address and go to that address. Make sure that only one of these interfaces is enabled, the presence of duplicate IP addresses in your network may have unintended behavior.

Solution 2. Make the router take care of which server is turned on. You will need to write a program to run on the router (or on another server), which can determine which server is turned on and forward the packets accordingly. If you use Linux, then iptables programs are worth a look.

0
source

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


All Articles