BSD Sockets Interface Programmer's Guide

Chapter 6 129
Using UNIX Domain Stream Sockets
Writing the Server Process
Binding a Socket Address to the Server
Process's Socket
After your server process has created a socket, it must call bind to bind a
socket address. Until an address is bound to the server socket, other
processes have no way to reference it.
The server process must bind a specific pathname to this socket, which
is used for listening. Otherwise, a client process would not know what
pathname to connect to for the desired service.
Set up the address structure with a local address before you make a
bind call. bind and its parameters are described in the following table.
Include files: #include <sys/types.h>
#include <sys/un.h>
#include <sys/socket.h>
System call: bind (s, addr, addrlen) int s; struct
sockaddr_un *addr; int addrlen;
Function result: 0 if bind is successful, –1 if failure occurs.
Example: struct sockaddr_un myaddr;
...
bind (ls, &myaddr, sizeof(struct sockaddr_un));
When to Bind Socket Addresses
The server process should bind socket addresses after the socket is
created and before any other BSD Sockets system calls. Refer to the
bind(2) man page for more information on bind.
Parameter
Description of
Contents
INPUT Value
s socket descriptor of local
socket
socket descriptor of socket
to be bound
addr socket address pointer to address to be
bound to s
addrlen length of socket address size of struct sockaddr_un