Identify the correct order in which a server process must invoke the function…

2015

Identify the correct order in which a server process must invoke the function calls accept, bind, listen, and recv according to UNIX socket API.

  1. A.

    listen, accept, bind, recv

  2. B.

    bind, listen, accept, recv

  3. C.

    bind, accept, listen, recv

  4. D.

    accept, listen, bind, recv

Attempted by 112 students.

Show answer & explanation

Correct answer: B

Correct order: bind, listen, accept, recv

  • bind: Associate the socket with a local address and port so the server has an endpoint to accept connections on.

  • listen: Mark the bound socket as passive and specify a backlog; this tells the OS to queue incoming connection requests.

  • accept: Remove the next pending connection from the queue and return a new connected socket specific to that client.

  • recv: Read data from the connected socket returned by accept (not from the listening socket).

Key warning: accept requires a listening socket, and recv requires a connected socket. Calling these functions in the wrong order will cause errors or incorrect behavior.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir