DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with sockets

Socket creation

The socket system call is used to create a socket:

   s = socket(domain, type, protocol);
This call requests that the system create a socket in the specified domain and of the specified type. If the protocol is left unspecified (a value of 0), the system will select an appropriate protocol from those that comprise the domain and that may be used to support the requested socket type. A descriptor (a small integer) that may be used in later system calls that operate on sockets is returned. The domain is specified as one of the manifest constants defined in the file sys/socket.h. For the UNIX domain the constant is AF_UNIX; for the Internet domain, it is either AF_INET or AF_INET6.


NOTE: The constants named AF_whatever show the address format to use in interpreting names.

The socket types are also defined in sys/socket.h and one of SOCK_STREAM, SOCK_DGRAM, or SOCK_RAW must be specified. To create a stream socket in the Internet domain the following call might be used:

   s = socket(AF_INET, SOCK_STREAM, 0);
This call would result in a stream socket being created with the TCP protocol providing the underlying communication support. To create a datagram socket for on-machine use the call might be:
   s = socket(AF_UNIX, SOCK_DGRAM, 0);
The default protocol (used when the protocol argument to the socket call is 0) should be correct for most situations. However, it is possible to specify a protocol other than the default; this will be covered in ``Advanced topics''.

A socket call may fail for several reasons. Aside from the rare occurrence of lack of memory (ENOBUFS), a socket request may fail because the request is for an unknown protocol (EPROTONOSUPPORT), or because the request is for a type of socket for which there is no supporting protocol (EPROTOTYPE).


© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005