Socket Creation
The socket
()
call creates a socket in the specified family and of
the specified type.
s = socket(family, type, protocol);
If the protocol is unspecified, the system selects a protocol that supports the requested socket type. The socket handle is returned. The socket handle is a file descriptor.
The family is specified by one of the constants that
are defined in sys/socket.h
. Constants that are named
AF_
suite specify the address
format to use in interpreting names:
-
AF_APPLETALK
-
Apple Computer Inc. Appletalk network
-
AF_INET6
-
Internet family for IPv6 and IPv4
-
AF_INET
-
Internet family for IPv4 only
-
AF_PUP
-
Xerox Corporation PUP internet
-
AF_UNIX
-
UNIX file system
Socket types are defined in
sys/socket.h
. These types,
SOCK_STREAM
, SOCK_DGRAM
, or
SOCK_RAW
, are supported by
AF_INET6
, AF_INET
, and
AF_UNIX
. For more information, see the
socket
(3C) man page.
The following example creates a stream socket in the Internet family:
s = socket(AF_INET6, SOCK_STREAM, 0);
This call results in a stream socket. The TCP protocol provides the underlying
communication. Set the protocol argument to
0
, the default, in most situations. You can specify a
protocol other than the default, as described in Advanced Socket Topics.