3

Let's say we have two packets A and B.

A: Has source-IP S1 , destination-IP D1 , source-port SP1 , destination-port DP1

B: Has source-IP S2 , destination-IP D1 , source-port SP2 , destination-port DP1

Both are TCP. Will both these packets have the same socket now?


About UDP I've read that its socket consists of the destination-IP and destination-port-number. Soo if this was done with UDP, both packets would be received by the same socket.

But this example is for TCP and here I'm not sure if it would also go through the same socket for TCP?

1
13

First, TCP does not care about single packets. If these are just data packets without any previous connection establishment than they will be simply dropped, no sockets involved. So I'm assuming that this is about established connections, or initial packets to establish a connection. A TCP connection is defined at least by the 4 tuple of (src-ip, src-port, dst-ip, dst-port). Since these 4 tuples are obviously different in both cases there will be different sockets needed.

About UDP I've read that its socket consists of the destination-IP and destination-port-number.

This is only true for unconnected UDP sockets. With connected UDP sockets again the 4-tuple is relevant.

3
  • There is one more question that just came to my mind, I hope it's alright if I ask it here. If those 4 tuples above were all the samein both cases, then we could use the same socket for them. In total, how many sockets would we have then? This socket + 2 more sockets because we have 2 clients, so 3 sockets in total for this?
    – kathelk
    Dec 14 '20 at 21:28
  • 2
    @kathelk: If the 4 tupel is the same then these packets belong to the same connection. These cannot be 2 clients for the same connection, but it must be the same client. If the sequence numbers don't fit then the non-fitting packets will be dropped. Because there is only one connection there is exactly one socket involved at each peer of the connection, so 2 in total. Dec 14 '20 at 21:36
  • Alright thank youu! :))
    – kathelk
    Dec 14 '20 at 21:47
4

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:

  1. One socket that uses listen() and accept() to wait for incoming TCP connections.
  2. For each incoming TCP connection one socket that is returned by accept().

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 access() API.

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.

1
  • You should emphasize that the meaning of "socket" is different in the API and the actual transport-layer protocol. While BSD-style API sockets are similarly used with UDP and TCP, UDP doesn't actually use a "socket" concept in the protocol.
    – Zac67
    Dec 15 '20 at 9:39
3

That depends on whose definition of socket you use and in some cases whether the packet is opening a new connection or transferring data for an existing connction.

The RFC that defines TCP defines the term socket as a combination of an IP address and a port. By this definition the two packets have the same socket on the server side but different sockets on the client side.

However the "sockets API"* does not use this definition. The sockets API uses the term socket to refer to a communications object provided by the operating system to applications. To avoid confusion in the rest of this post I will use the term "socket object" to refer to what the sockets API calls a socket.

When you start a server it creates a socket object, binds it to the desired local address/port and tells the operating system to start listing for connections. The address specified may be an individual local address or it may be a wildcard to listen on all local addresses (there are some subtleties about IPv6 which I won't get into here). The remote address and port fields of this socket object are wildcards allowing it to accept connection requests from anywhere.

When the server accepts a connection the operating system creates a new socket object to represent the new connection. This socket object has a full complement of address and port fields filled in.

* Sometimes known as Berkeley sockets or Posix sockets. This API originated in BSD, and is standardized by posix. This API or a variant of it is used for IP communication on all major operating systems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.