Suppose 3 laptops are dialed into a Wi-Fi.
Now someone sends a server request to the IP + port.
How does the router know for which device the packages are intended?
Can a socket which is bound by a program only ever be connected to one port?
How does the router know for which device the packages are intended?
The router doesn't even notice anything. Communication in the same network does not go through the router.
Can a socket which is bound by a program only ever be connected to one port?
A socket always has 4 identifying features.
Source IP
Target IP
Source port
Destination port
So it is always the same socket if all 4 values match.
An application A can become an application B, for example. Communicate from port 3252 to port 80 and at the same time to an application C on the same ports, provided that C has a different IP address than B
To understand the answer to your question, you first have to realize that the LAN in which the laptops are is not the Internet.
Your devices can't be reached from the Internet with the IP address of the LAN.
If so, your question does not arise.
If you want to set this up, then you have to use the router's Internet IP address and program a port forwarding there (in the router).
Say: Port 8080 goes to IP 192.168.1.50
Note: This primarily affects IPv4 networks. But also basically applies to IPv6.
So if I use the createsocket () in C, a handle of 4 is created and if the network card receives a request for this handle does the router route this to the program?
Or can it be that 2 computers then receive a server request? Or does a PC register as a user of Port XY?
That means if 3 laptops share an IP in the LAN, then does a PC have to "claim" a port for communication or rather a program on the PC?
If 3 laptops would share an IP, you would have a nice mess in your LAN. Please note the first sentence of my answer - otherwise nothing will come with understanding how it works.
Ok, addendum, because I only now understand exactly what you mean
So you have laptops A, B, C in a network. Laptop B wants to call Google.
The router only has one public address. So how does the router know from Google's response that it has to go to laptop B and not to A or C, because EVERYONE is behind the public IP.
Basically it is very simple. Every time a client connects to a server, the source port (unless it is explicitly set manually) is set to a random value. The router, which has to forward the packet, remembers this source port and says: Ok, behind it is now laptop B. If the answer comes to exactly this socket, the router knows who the answer should go to.
Example:
Laptop B -> Google
Sockets look like this:
Laptop B
Source IP: 192.168.178.10 (internal IP of the laptop)
Destination IP: 216.58.207.35 (one of Google's public ones)
Source port: 23535 (randomly generated by the system)
Destination port: 443 (standard port HTTPS)
If you go on the Internet, then of course it goes through the router. The router now does the following: It changes the source IP to its public IP. (Source NAT, or masquerading called)
With Google the socket looks like this:
Source IP: 216.58.207.35
Target IP: your public ip
Source port: 443
Destination port: 23535
If the answer comes from Google, the router on port 23535 knows that it should go to laptop B. In addition, the router changes the destination IP from its public to the private one from laptop B (destination NAT)
I think I found out exactly what the questioner wanted to know. Its connection only has a public IP. The "share" these practically, even if none of his laptops has assigned the public address anywhere. In my answer, I once again commented on how the router knows when a reply from the Internet to which client behind it the answer should go.
Alright. Thank you, Captain Obvious! 😉
The problem at the moment is "WAN is not LAN".
This understanding is essential when setting up port forwarding.
Pah, at least I'm a captain.
I read on Socket Wiki articles that the "localhost" is also part of the sockaddr_in struct! Can't it be separated like this? Everyone has their own localhost in Ethernet?
And: what if two programs want to google at the same time? Then don't they bother each other because every other destination port is received by the OS? Is the assignment of the port done after asking the router whether the degree is free?
Localhost is just a host name for the IP 127.0.0.1. And this in turn can only be used within a computer to address itself.
And: what if two programs want to google at the same time?
Since a random source port is always used and it is unlikely that it will be the same for 2 requests that do not interfere.
The best thing to do is buy a book: https://www.amazon.de/...07S8ZJMVL/
Especially when it comes to networking, it makes sense to work from the bottom up from the basics.
The router is only interested in the IP address. Only the recipient looks at the port to determine the associated application.
If you think that the three laptops in the WLAN and the server in the same network, e.g. B. In the home network, you do not need a router. Only access point and switch are used. In the home network, these are integrated in one device, which we call a WLAN router. However, the routing instance is not used here. Neither access point nor switch are interested in IP addresses, ports or sockets. You just look for MAC addresses. The data now ends up at the device with the correct MAC address, which must then look into the TCP or UDP port and process the data correctly.
In any case, the socket or port is completely uninteresting for transport and delivery.
So to put it simply:
Suppose I want to establish a connection to a PC that is in the network with the IP address 194.168.1.50.
And there are 2 computers A and B.
And computer A has a server running and listened on port 55000.
Then I send:
My IP, my port, the IP of the computer and port 55000 and land on the router on computer A? While knows which computer has reserved this port?