Search code examples
tcpudp

How UDP Socket handle the diff host


Suppose a process in Host C has a UDP socket with port number 6789. Suppose both Host A and Host B each send a UDP segment to Host C with destination port number 6789. Will both of these segments be directed to the same socket at Host C? If so, how will the process at Host C know that these two segments originated from two different hosts?

Solution Provided by book is as follows

Yes, both segments will be directed to the same socket. For each received segment, at the socket interface, the operating system will provide the process with the IP addresses to determine the origins of the individual segments.

  1. i don't get the solution can anyone elaborate it ?

  2. What can be the answer if the word "UDP" is changed by "TCP" ?


Solution

  • Will both of these segments be directed to the same socket at Host C?

    If the socket on host C is not explicitly connected, then both the packet from host A and the packet from host B will arrive at this socket.

    If so, how will the process at Host C know that these two segments originated from two different hosts?

    Using recvfrom the client not only gets the packet payload but also the sender of the packet.

    What can be the answer if the word "UDP" is changed by "TCP" ?

    TCP has the concept of a connection, which involves the explicit creation of a connection, data transfer and explicit connection tear down. A and B each must create a connection to C first before they can transfer data. To make this possible C must first listen on the bound server socket and then can accept a new connection. accept will return a new socket which is used for the data transfer. It will also return the clients source IP and port (i.e. A or B) so that C knows which client is associated with the new connection. This can also be later checked on the accepted socket with getpeername.