System Interface Guide

Socket Creation and Naming

socket(3N) is called to create a socket in the specified domain and of the specified type. If a protocol is not specified, the system defaults to a protocol that supports the specified socket type. The socket handle (a descriptor) is returned.

A remote process has no way to identify a socket until an address is bound to it. Communicating processes connect through addresses. In the UNIX domain, a connection is usually composed of one or two path names. In the Internet domain, a connection is composed of local and remote addresses and local and remote ports. In most domains, connections must be unique.

bind(3N) is called to bind a path or internet address to a socket. There are three different ways to call bind(3N), depending on the domain of the socket. For UNIX domain sockets with paths containing 14, or fewer characters, you can


#include <sys/socket.h>
 ...
 	bind (sd, (struct sockaddr *) &addr, length);

If the path of a UNIX domain socket requires more characters, use


#include <sys/un.h>
 ...
 	bind (sd, (struct sockaddr *) &addr, length);

And for Internet domain sockets, use


#include <netinet/in.h>
 ...
 	bind (sd, (struct sockaddr *) &addr, length);

In the UNIX domain, binding a name creates a named socket in the file system. Use unlink(2) or rm(1) to remove the socket.