Socket(concept)

From LQWiki
Jump to: navigation, search

Sockets are points where connections are made. In unix, they have been generalized to handle any connection.

A server program normally requests that a socket be created, specifying the 'domain' (also known as 'protocol family'), 'type', and 'protocol'. Then the socket is given an address with 'bind', told to wait for connections with 'listen', and when a connection attempt is made, it is told to start a conversation with 'accept'.

A client program like a web browser requests that a socket be created in the same way, but then the socket is told to 'connect' to another socket by name.

The currently understood protocol families are:

      AF_UNIX, AF_LOCAL   Local communication     
      AF_INET             IPv4 Internet protocols   
      AF_INET6            IPv6 Internet protocols      
      AF_IPX              IPX - Novell protocols
      AF_NETLINK          Kernel user interface device  
      AF_X25              ITU-T X.25 / ISO-8208 protocol 
      AF_AX25             Amateur radio AX.25 protocol
      AF_ATMPVC           Access to raw ATM PVCs
      AF_APPLETALK        Appletalk                     
      AF_PACKET           Low level packet interface  

The currently defined types are:

      SOCK_STREAM     Provides sequenced, reliable, two-way, connection-based
                      byte  streams.  An out-of-band data transmission mecha‐
                      nism may be supported.
      SOCK_DGRAM      Supports datagrams (connectionless, unreliable messages
                      of a fixed maximum length).
      SOCK_SEQPACKET  Provides  a  sequenced,  reliable,  two-way connection-
                      based data transmission path  for  datagrams  of  fixed
                      maximum  length;  a  consumer  is  required  to read an
                      entire packet with each input system call.
      SOCK_RAW        Provides raw network protocol access.
      SOCK_RDM        Provides a reliable datagram layer that does not  guar‐
                      antee ordering.
      SOCK_PACKET     Obsolete  and  should  not be used in new programs; see
                      packet(7).

Some socket types may not be implemented by all protocol families; for example, SOCK_SEQPACKET is not implemented for AF_INET. Normally, only a single protocol exists for a given socket type in a protocol family.

Web servers set up a socket of protocol family AF_INET, type SOCK_STREAM, protocol 0, then bind the socket to port 80 of the default host (0.0.0.0), then listen for connections, and finally accept any connections as they occur. Browsers set up the same kind of socket, but then connect to port 80 of the desired host (1.2.3.4). Once these steps are finished, the programs are able to exchange information.

Sockets of type AF_UNIX appear as files on the local computer.