Seth Woolley's Man Viewer

Manual for socket - man 2 socket

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

SOCKET(2)                  Linux Programmer's Manual                 SOCKET(2)

       socket(2,7,n) - create an endpoint for communication

       #include <sys/types.h>
       #include <sys/socket.h>

       int socket(2,7,n)(int domain, int type, int protocol);

       Socket  creates an endpoint for communication and returns a descriptor.

       The domain parameter specifies a communication domain; this selects the
       protocol  family  which will be used for communication.  These families
       are  defined  in(1,8)  <sys/socket.h>.   The  currently  understood  formats

       Name                Purpose                          Man page
       PF_UNIX, PF_LOCAL   Local communication              unix(7)
       PF_INET             IPv4 Internet protocols          ip(7,8)(7)
       PF_INET6            IPv6 Internet protocols
       PF_IPX              IPX - Novell protocols
       PF_NETLINK          Kernel user interface device     netlink(3,7)(7)
       PF_X25              ITU-T X.25 / ISO-8208 protocol   x25(7)
       PF_AX25             Amateur radio AX.25 protocol
       PF_ATMPVC           Access to raw(3x,7,8,3x cbreak) ATM PVCs
       PF_APPLETALK        Appletalk                        ddp(7)
       PF_PACKET           Low level packet interface       packet(7)

       The  socket(2,7,n)  has  the indicated type, which specifies the communication
       semantics.  Currently defined types are:

              Provides sequenced,  reliable,  two-way,  connection-based  byte
              streams.  An out-of-band data transmission mechanism may be sup-

              Supports datagrams (connectionless,  unreliable  messages  of  a
              fixed maximum length).

              Provides  a  sequenced,  reliable, two-way connection-based data
              transmission path for datagrams of fixed maximum length; a  con-
              sumer is required to read(2,n,1 builtins) an entire packet with each read(2,n,1 builtins) system

              Provides raw(3x,7,8,3x cbreak) network protocol access.

              Provides a reliable  datagram  layer  that  does  not  guarantee

              Obsolete  and should not be used in(1,8) new programs; see packet(7).

       Some socket(2,7,n) types may not be implemented by all protocol families;  for
       example, SOCK_SEQPACKET is not implemented for AF_INET.

       The  protocol  specifies  a  particular  protocol  to  be used with the
       socket.  Normally only a single protocol exists to support a particular
       socket(2,7,n)  type  within  a given protocol family, in(1,8) which a case protocol
       can be specified as 0.  However, it is possible that many protocols may
       exist,  in(1,8)  which  case a particular protocol must be specified in(1,8) this
       manner.  The protocol number to use is specific to  the  "communication
       domain" in(1,8) which communication is to take place; see protocols(5).  See
       getprotoent(3) on how to map protocol name strings to protocol numbers.

       Sockets  of  type  SOCK_STREAM are full-duplex byte streams, similar to
       pipes.  They do not preserve record boundaries. A stream socket(2,7,n) must be
       in(1,8)  a connected state before any data may be sent or received on it.  A
       connection to another socket(2,7,n) is created with a connect(2)  call.   Once
       connected,  data may be transferred using read(2,n,1 builtins)(2) and write(1,2)(2) calls or
       some variant of the send(2,n)(2) and recv(2) calls.  When a session has been
       completed  a  close(2,7,n)(2)  may be performed.  Out-of-band data may also be
       transmitted as described  in(1,8)  send(2,n)(2)  and  received  as  described  in(1,8)

       The  communications protocols which implement a SOCK_STREAM ensure that
       data is not lost or duplicated.  If a piece of data for which the  peer
       protocol  has  buffer space cannot be successfully transmitted within a
       reasonable length of time(1,2,n), then the  connection  is  considered  to  be
       dead.   When  SO_KEEPALIVE is enabled on the socket(2,7,n) the protocol checks
       in(1,8) a protocol-specific manner if(3,n) the other end is still alive.  A  SIG-
       PIPE  signal(2,7)  is  raised  if(3,n)  a  process  sends or receives on a broken
       stream; this causes naive processes, which do not handle the signal(2,7), to
       exit.    SOCK_SEQPACKET   sockets  employ  the  same  system  calls  as
       SOCK_STREAM sockets.  The only difference is that  read(2,n,1 builtins)(2)  calls  will
       return  only  the  amount  of  data requested, and any remaining in(1,8) the
       arriving packet will be  discarded.  Also  all  message  boundaries  in(1,8)
       incoming datagrams are preserved.

       SOCK_DGRAM  and  SOCK_RAW  sockets allow sending of datagrams to corre-
       spondents named(5,8) in(1,8) send(2,n)(2) calls.   Datagrams  are  generally  received
       with  recvfrom(2),  which  returns  the  next  datagram with its return

       SOCK_PACKET is an obsolete socket(2,7,n) type to receive raw(3x,7,8,3x cbreak) packets  directly
       from the device driver. Use packet(7) instead.

       An  fcntl(2) call with the the F_SETOWN argument can be used to specify
       a process group to receive a SIGURG signal(2,7) when  the  out-of-band  data
       arrives  or  SIGPIPE  signal(2,7) when a SOCK_STREAM connection breaks unex-
       pectedly.  It may also be used to set(7,n,1 builtins) the process or process group that
       receives the I/O and asynchronous notification of I/O events via SIGIO.
       Using F_SETOWN is equivalent to an ioctl(2) call with the FIOSETOWN  or
       SIOCSPGRP argument.

       When  the  network  signals  an  error(8,n) condition to the protocol module
       (e.g.  using a ICMP message for IP) the pending error(8,n) flag is  set(7,n,1 builtins)  for
       the  socket.   The  next operation on this socket(2,7,n) will return the error(8,n)
       code of the pending error. For some protocols it is possible to  enable
       a  per-socket  error(8,n)  queue(1,3)  to retrieve detailed information about the
       error(8,n); see IP_RECVERR in(1,8) ip(7,8)(7).

       The operation of sockets is controlled by socket(2,7,n) level options.   These
       options are defined in(1,8) <sys/socket.h>.  The functions setsockopt(2) and
       getsockopt(2) are used to set(7,n,1 builtins) and get options, respectively.

       -1 is returned if(3,n) an error(8,n) occurs; otherwise  the  return  value  is  a
       descriptor referencing the socket.

       EACCES Permission  to create a socket(2,7,n) of the specified type and/or pro-
              tocol is denied.

              The implementation does not support the specified  address  fam-

       EINVAL Unknown protocol, or protocol family not available.

       EMFILE Process file(1,n) table overflow.

       ENFILE The  system  limit  on  the  total number of open(2,3,n) files has been

              Insufficient memory is available.  The socket(2,7,n) cannot be  created
              until sufficient resources are freed.

              The  protocol  type  or  the specified protocol is not supported
              within this domain.

       Other errors may be generated by the underlying protocol modules.

       4.4BSD, SUSv2, POSIX 1003.1-2001.  The socket(2,7,n) function call appeared in(1,8)
       4.2BSD.  It  is  generally  portable to/from non-BSD systems supporting
       clones of the BSD socket(2,7,n) layer (including System V variants).

       The manifest constants used under BSD 4.*  for  protocol  families  are
       PF_UNIX,  PF_INET,  etc., while AF_UNIX etc. are used for address fami-
       lies. However, already the BSD man(1,5,7) page promises: "The protocol  family
       generally  is the same as the address family", and subsequent standards
       use AF_* everywhere.

       SOCK_UUCP is not implemented yet.

       accept(2,8)(2),  bind(2,n,1 builtins)(2),  connect(2),  fcntl(2),  getpeername(1,2)(2),  getsock-
       name(2),   getsockopt(2),   ioctl(2),   listen(1,2,7)(2),   read(2,n,1 builtins)(2),  recv(2),
       select(2,7,2 select_tut)(2),  send(2,n)(2),  shutdown(2,8)(2),  socketpair(2),  write(1,2)(2),   getpro-
       toent(3), ip(7,8)(7), socket(2,7,n)(7), tcp(7), udp(7), unix(7)

       "An  Introductory  4.3  BSD  Interprocess  Communication  Tutorial"  is
       reprinted in(1,8) UNIX Programmer's Supplementary Documents Volume 1.

       "BSD Interprocess Communication Tutorial" is reprinted in(1,8) UNIX Program-
       mer's Supplementary Documents Volume 1.

Linux 2.6.7                       2004-06-17                         SOCKET(2)

References for this manual (incoming links)