Transport Interfaces Programming Guide

Socket Creation

The socket() call creates a socket in the specified domain and of the specified type.


s = socket(domain, type, protocol);

If the protocol is unspecified (a value of 0), the system selects a protocol that supports the requested socket type. The socket handle (a file descriptor) is returned.

The domain is specified by one of the constants defined in sys/socket.h. Constants named AF_suite specify the address format to use in interpreting names as shown in Table 2-1.

Table 2-1 Protocol Family

AF_APPLETALK

Apple Computer Inc. Appletalk network 

AF_INET

Internet domain 

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_INET and AF_UNIX. The following creates a stream socket in the Internet domain:


s = socket(AF_INET, SOCK_STREAM, 0);

This call results in a stream socket with the TCP protocol providing the underlying communication. The following creates a datagram socket for intramachine use:


s = socket(AF_UNIX, SOCK_DGRAM, 0);

Use the default protocol (the protocol argument is 0) in most situations. You can specify a protocol other than the default, as described in "Advanced Topics".