2.2 The Socket API

I l @ ve RuBoard

The Socket API was developed in BSD UNIX [MBKQ96] to provide an application-level interface to the TCP/IP protocol suite. This API has since been ported to most operating systems, including all those supported by ACE. It's now the de facto standard for programming interprocess communication over TCP/IP.

Applications can use the C functions in the Socket API to create and manage local endpoints of communication, which are called sockets. Each socket is accessed via a handle , which is also referred to as a descriptor in the UNIX literature. A socket handle identifies a single communication endpoint maintained by the OS, while shielding applications from differences between, and dependencies on, low-level OS kernel implementation details, such as the following:

  • In UNIX, socket handles and other I/O handles, such as file, pipe, and terminal device handles, can be used interchangeably for most operations.

  • In Microsoft Windows, socket handles can't be used interchangeably with I/O handles for most operations, though they serve similar purposes.

Each socket can be bound to a local and a remote address. These addresses define the association between two or more peers that communicate via the socket.

The Socket API contains approximately two dozen system functions that can be classified into the following five categories:

  1. Local context management. The Socket API provides functions to manage local context information, which is normally stored within the OS kernel or in system libraries:

    Function Description
    Socket() A factory function that allocates a socket handle and returns it to the caller.
    bind() Associates a socket handle with a local or remote address.
    getsockname() Returns the local address to which a socket is bound.
    getpeername() Returns the remote address to which a socket is bound.
    close() Deallocates a socket handle, making it available for reuse.
  2. Connection establishment and connection termination. The Socket API provides functions to establish and terminate connections:

    Function Description
    Connect() Establishes a connection actively on a socket handle.
    listen() Indicates its willingness to listen passively for incoming client connection requests .
    Accept() A factory function that creates a new communication endpoint to service client requests.
    shutdown() Selectively terminates the read-side and/or write-side stream of a bidirectional connection.
  3. Data transfer mechanisms. The Socket API provides functions to send and receive data via socket handles:

    Function Description
     send()recv() 
    Transmit and receive buffers of data via a particular I/O handle.
     sendto()recvfrom() 
    Exchanges connectionless datagrams, where each sendto() call provides the networking address of the recipient.

    On UNIX, these functions can also be used for other types of 1/O handles, such as files and terminal devices. UNIX platforms also provide the following data transfer mechanisms:

    Function Description
     read()write() 
    Receive and transmit buffers of data via a particular handle.
     readv()writev() 
    Supports scatter-read and gather-write semantics, respectively, to optimize mode switching and simplify memory management.
     sendmsg()recvmsg() 
    General-purpose functions that subsume the behavior of the other data transfer functions.
  4. Options management. The Socket API defines functions that allow programmers to alter default socket behavior to enable multicasting, broadcasting, and modifying/querying the size of transport buffers:

    Function Description
    setsockopt() Modifies options in different protocol stack layers .
    setsockopt() Queries options in different protocol stack layers.
  5. Network addressing. In addition to the functions described above, networked applications often use functions to resolve humanly readable names , such as tango.ece.uci.edu , to low-level network addresses, such as 128.195.174.35 :

    Function Description
     gethostbyname() gethostbyaddr() 
    Handle network address mapping between hostnames and IPv4 addresses.
     getipnodebyname() getipnodebyaddr() 
    Handle network address mapping between hostnames and IPv4/IPv6 addresses.
    get servbyname() Identifies services by their humanly readable names.

Although the Socket API is most often used to write TCP/IP applications it's broad enough to support multiple communication domains. A communication domain is defined by a protocol family and an address family, as follows :

  • Protocol family. Today's networking environments include a large number of protocols that can offer a variety of communication services, such as connection-oriented reliable delivery, unreliable multicast, etc. A protocol family is a collection of protocols that offers a distinct set of related services. When creating a socket using the Socket API, the protocol is specified by a combination of the following two parameters:

    1. Protocol family ” for example, UNIX-domain (PF_UNIX), Internet-domain IPv4 (PF_INET) and IPv6 (PF_INET6), ATM (PF_ATMSVC), X.25 (PF_x25), Appletalk (PF_APPLETALK), and so on.

    2. Service type ” for example, sequenced , reliable bytestream (SOCK_STREAM), unreliable datagram (SOCK_DGRAM), and so on.

    For example, the TCP/IP protocol is specified by passing the PF_INET (or PF_INET6) and SOCK_STREAM flags to the socket() function.

  • Address family. An address family defines an address format that characterizes the size of an address in bytes, as well as the number, type, and order of its fields. In addition, an address family defines a set of functions that interpret the address format, for example, to determine the subnet where an IP datagram is destined. Address families correspond closely to protocol families, for example, the IPv4 address family AF_INET works only with the IPv4 protocol family PF_INET.

I l @ ve RuBoard


C++ Network Programming
C++ Network Programming, Volume I: Mastering Complexity with ACE and Patterns
ISBN: 0201604647
EAN: 2147483647
Year: 2001
Pages: 101

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net