5
votes

What I am asking is if two computers listen to the same port and a packet of information enters the router through the WAN Ip and the same port. Would the packet go to both computers? Neither? One or the other?

IE

computer 1 -(internal IP)-> 192.168.1.3 -(listens to port)-> 4444

computer 2 -(internal IP)-> 192.168.1.2 -(listens to port)-> 4444

computer 3 -(connects and sends)-> 24.157.358.45:4444

packet -> computer 1 AND computer 2

The code in VB6 is:

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

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

If there is something that needs to be clarified I would be more than happy to.

4

4 Answers

5
votes

The router won't send an inbound packet to either machine unless communication has already been established.

If 192.168.1.3 calls out to some other machine (e.g. 4.5.6.7) from its port 4444, the router will assign an arbitrary port on its external address (say 24.157.358.45 [sic] :5555) and pass the packets on to 4.5.6.7. 4.5.6.7 will send reply packets to 24.157.358.45:5555 -- because that's the only address it knows about -- and the router will relay those to 192.168.1.3:4444.

That's the normal course of things, but there are a lot of additional details to this scheme that make it possible to establish communication with a machine behind a router via trickery.

The system of having machines with private IP addresses behind a router with a public address is called network address translation (NAT); it's a pretty deep topic.

4
votes

From my knowledge of routers, unless port forwarding is setup, the router will discard any packets sent on that port.

If port forwarding is setup, only one of the computers could be setup to receive the packets.

3
votes

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

Once a TCP connection is established, the router knows which specific LAN machines are associated with which connections and will route incoming packets belonging to those connections accordingly.

2
votes

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 sounds like you have multiple servers and want to forward to whatever server is turned on at the moment. This is not possible (*), the router does not care whether or not PC1 or PC2 are listening on port 4444, it will simply forward everything to the address configured in the port forwarding.

(*): Ok it is possible but it takes some extra work.

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

Solution 2: Make the router care about which server is on. You will need to write a program to run on the router (or on another server) that can detect which server is on and forward the packets accordingly. If you are using Linux the program iptables can be worth looking at.