The socket address specifies the communication family. UNIX domain sockets are defined as sockaddr_un. Internet domain sockets are defined as
sockaddr_in or
sockaddr_in6 for IPv6.
The socket() system call creates one end of the socket.
The socket() system call returns the socket descriptor, a small integer that is similar to the file descriptor used in other system calls. For example:
The bind() system call associates an address with the socket descriptor.
In the sockaddr structure for IPv4 sockets, the first field specifies AF_INET. The second field
sin_port can be any integer > 5000. Lower port numbers are reserved for specific services. The third field
in_addr is the Internet address in dotted-quad notation. For the server, you can use the constant
INADDR_ANY to tell the system to accept a connection on any Internet interface for the system. Conversion functions
htons() and
htonl() are for hardware independence. For example:
The listen() system call prepares a connection-oriented server to accept client connections.
The accept() system call initiates communications between a connection-oriented server and the client.
Generally programs call accept() inside an infinite loop, forking a new process for each accepted connection. After
accept() returns with client address, the server is ready to accept data.
On the client, the connect() system call establishes a connection to the server.
This is similar to the accept() system call, except that the client does not have to bind a local address to the socket descriptor before calling
connect(). The server address pointed to by
srv_addr must exist.