Lecture Notes for Client-Server Interfaces

31 January 2002 - The Sockets API


  1. application program interface

    1. how an application program gets access to os, or any, services

    2. there are many apis - differ in form and use; sockets; tli-xli; plan 9

    3. good programmers immediately hide the api - wrappers, libraries; efficiency

  2. the sockets api

    1. developed at berkeley in the early 80s; 4.1 bsd

    2. the evils of too much generality - protocols, addresses

    3. re-implemented (ported) many times - winsock on windows

    4. many other sockets-like and sockets-based apis - shells, scripting languages, libraries

  3. general characteristics

    1. there is a difference between

      1. connection-oriented and connection-less communication - different set-up, use, and tear-down

      2. clients and servers - different system calls, asymmetric behaviors (not like pipes)

    2. socket manipulation is detailed - many steps, with different detail at each step

    3. many api routines (18 socket specific), almost all used.

  4. basic approach - set-up, communicate, shut-down (tcp oriented)

    1. clients

      1. create a socket (the active socket)

      2. connect to the server

      3. communicate

      4. shut-down, or close, or tear-down

    2. servers

      1. create a socket (the listener or the passive socket)

      2. give the socket an endpoint address (binding)

      3. wait for client requests

      4. create a new socket for each request (the active socket)

      5. communicate over the new connection

      6. shutdown the new connection

  5. endpoints and connections

    1. an endpoint is a three-tuple (ip addr, port no, protocol).

      1. undefined - no ip addr or port no

      2. unresolved - wildcard ip addrs or port nos, servers

      3. resolved - a specific ip addr and port no

    2. a connection is a pair of fully resolved endpoint addresses - a five-tuple, because protocols must match; udp included; local and remote (or foreign) endpoints

    3. the two questions to understand the sockets interfaces

      1. when does an endpoint become resolved (what is the state of an endpoint at any time)

      2. when do endpoints get paired into a connection (and how)

  6. the set-up api

    1. socket() - create a socket with the undefined endpoint (?, ?, p)

    2. bind() - assign an ip addr and port no to a socket; the local part of a connection pair; may be unresolved; client and server optional

    3. connect() - resolve both endpoints in a connection; clients only

    4. listen() - resolve the local endpoint; make it a listener; establish the connection queue; server only

    5. accept() - retrieve the next connection from the connection queue; returns a new socket with a new connection and a new, resolved local endpoint; server only; blocks on empty queues

  7. the shut-down api

    1. closing is not the same as shutting down - full duplex connection; data draining from the network; shared data among processes

    2. close() - reference counted, full duplex, recovers resources

    3. shutdown() - per process, half duplex, does not recover resources

  8. the communication api

    1. where to send on connection-less communication - sender identifies receiver

    2. read() and write() - the usual

    3. recvfrom() and sendto() - remote endpoint information


This page last modified on 7 February 2002.