About UDP I've read that ...
The main difference between TCP and UDP is that TCP is connection-oriented while UDP is used to transmit single packets.
A UDP socket is handling all UDP packets arriving at a certain (destination) port on the computer.
A TCP socket is handling all packets belonging to a certain TCP connection.
Will both these packets have the same socket now?
It's more complicated than you think:
The situation you describe typically happens on the "server-side" (on the computer that uses the
accept() API to handle incoming connections - such as a web server).
Let's say two computers (running web browsers) want to connect to the same web server. Both computers chose the same "source TCP port". In this case the packets sent by the two computers to the web server satisfy your condition: Only the source IP address differs in the packets sent by the two computers while the destination IP address, the destination port and the source port are the same.
However, on the "server-side" two sockets are involved in handling a single TCP connection:
- One socket that uses
accept() to wait for incoming TCP connections.
- For each incoming TCP connection one socket that is returned by
When a TCP packet is the first packet of a TCP connection, the packet is "handled" by the TCP socket that is performing a
listen() on the destination port.
This socket does not care about the source IP address nor the source TCP port - just like UDP sockets. The socket is handling all first packets of TCP connections having a certain destination IP address and a certain destination port.
When that first packet is received, the second socket (handling the TCP connection) is created and returned by the
This socket handles a certain connection (and therefore all further packets of the TCP connection).
Because we have two different connections in the example with the two computers connecting to the same server using the same "source TCP port" two different sockets will handle packets that differ in destination TCP port only.