Linux programming interface

Linux Programming Interface


  • What is a connection, concretely?

    • From RFC 793:

    The reliability and flow control mechanisms described above require that TCPs initialize and maintain certain status information for each data stream. The combination of this information, including sockets, sequence numbers, and window sizes, is called a connection. Each connection is uniquely specified by a pair of sockets identifying its two sides.

  • What does a domain socket file look like from another OS reading off the same drive?



  • Syscalls (technically glibc functions wrapping a single syscall)

    • socket() to create a socket
    • bind() it to an address
    • listen() on that address
    • accept() a connection
    • Clients connect() to a bound socket
  • bind needs an address, but each type of socket uses a different address format.

    • The sockaddr struct serves as a generic cast destination for each domain-specific addr struct
  • listen’s backlog argument controls the number of unaccepted connections the kernel buffers before blocking clients attempting to connect

  • The key point to understand about accept() is that it creates a new socket, and it is this new socket that is connected to the peer socket that performed the connect(). A file descriptor for the connected socket is returned as the function result of the accept() call. The listening socket (sockfd) remains open, and can be used to accept further connections.

  • Comm over a socket with read & write (generic) or send & recv (socket-specific)

  • EOF indicates that the socket has been closed

  • Byte stream that doesn’t respect message boundaries, so a reader might read different “chunks” than were written.


UDP uses a different API:


  • UDP sockets can use connect to have the kernel remember it’s peer on the other end, which allows the use of read and write. This isn’t a real connection, though.

Although UNIX domain sockets are identified by pathnames, I/O on these sockets doesn’t involve operations on the underlying device.

for UNIX domain sockets, datagram transmission is carried out within the kernel, and is reliable. All messages are delivered in order and unduplicated.

  • Datagrams respect boundaries, so a read always corresponds to a specific write.

socketpair can be used to create two sockets that are connected to each other. Seems like a way to create an internal pipe without requiring a pathname.

TCP allows for out-of-band transmission, where (at most a single byte at a time) data can be marked urgent, and can be processed without having to go through intervening data. Used for aborts, etc. The urgent pointer is related to this.

TCP_CORK and TCP_NODELAY both buffer data before sending. CORK waits until a target buffer capacity is reached. NODELAY (Nagle) waits for an ACK.