Sockets

The socket as a communication end, the TCP stream and UDP datagrams, the server calls (socket, bind, listen, accept) and the client calls (socket, connect), send and recv, sendto and recvfrom, and network byte order.

  • 31 cards
  • 5 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. The numbers
  5. On the diagram

A few cards from the deck

  • Socket: what is it?

    Show the answer

    A communication endpoint handled through a descriptor, identified by an IP address, a port number and a transport protocol.

    Like a file or a pipe, it is read and written through descriptors. It is obtained with socket(AF_INET, SOCK_STREAM, 0) for TCP and SOCK_DGRAM for UDP.

  • What should replace the question mark to read what the client sent?

    ```c
    int ecoute = socket(AF_INET, SOCK_STREAM, 0);
    bind(ecoute, (struct sockaddr *)&adresse, sizeof adresse);
    listen(ecoute, 5);
    int client = accept(ecoute, NULL, NULL);
    char buf[100];
    ssize_t n = recv(?, buf, sizeof buf, 0);
    `

    • client
    • ecoute
    • ecoute, after a second accept
    • 0, the server's standard input
    Show the answer

    client

    accept returned client, the service socket for this exchange. The listening socket only serves to accept connections: calling recv on it gives the “Transport endpoint is not connected” error.

  • On a TCP server, the call order is socket, …, …, …; the client simply chains socket then connect.

    Show the answer

    On a TCP server, the call order is socket, bind, listen, accept; the client simply chains socket then connect.

    bind sets the local address, listen opens the waiting queue, accept takes a client from it. The client does not call bind: the system gives it an ephemeral port.