Consider socket API on a Linux machine that supports connected UDP sockets. A…

2017

Consider socket API on a Linux machine that supports connected UDP sockets. A connected UDP socket is a UDP socket on which connect function has already been called. Which of the following statements is/are CORRECT?

I.    A connected UDP socket can be used to communicate with multiple peers simultaneously.

II.   A process can successfully call connect function again for an already connected UDP socket.

  1. A.

    I only

  2. B.

    II only

  3. C.

    Both I and II

  4. D.

    Neither I nor II

Attempted by 99 students.

Show answer & explanation

Correct answer: B

Correct answer: the statement that a process can call connect again for an already connected UDP socket is correct, while the statement that a connected UDP socket can be used to communicate with multiple peers simultaneously is incorrect.

  • Why the multiple-peers statement is false: Connecting a UDP socket associates it with a single remote peer. send()/write() use that peer and the kernel delivers to recv()/read() only datagrams from that peer, so the socket cannot be used simultaneously for multiple remote peers.

  • Why the re-connect statement is true: On Linux you may call connect again on a connected UDP socket; the new connect replaces the previous peer. You can also disconnect (for example by connecting to AF_UNSPEC) if needed.

  • Practical note: To talk to many peers use multiple sockets or keep the socket unconnected and use sendto()/recvfrom() to specify addresses explicitly.

Explore the full course: Gate Guidance By Sanchit Sir