|
System Calls
Communication between sockets is made possible by using various system calls. Following are few of the system calls involving sockets:
- socket – Creates a socket.
- closes – destroys a socket.
- connect – creates a connection between two sockets.
- bind – labels a server socket with an address.
- listen – configures a socket to accept conditions.
- accept – Accepts a connection and creates a new socket for the connection.
Sockets are represented by file descriptors.
Calling connect
To create a connection between two sockets, the client calls connect, specifying the address of a server socket to connect to. A client is a process initiating the connection, and a server is the process waiting to accept connections. The client calls connect to initiate a connection from a local socket to the server socket specified by the second argument. The third argument is the length, in bytes, of the address structure pointed to by the second argument. Socket address formats differ according to the socket namespace.
Servers
A server’s lifecycle consists of the creation of a connection-style socket, binding an address to its socket, placing calls to accept incoming connections, and then closing the socket. Data isn’t read and written directly via the server socket; instead, each time a program accepts a new connection. Linux creates a separate socket to use in transferring data over that connection.
An address must be bound to the server’s socket using bind if a client is to find it. Its first argument is the socket file descriptor. The second argument is a pointer to a socket address structure; the format of this depends on the socket’s address family. The third argument is the length of the address structure, in bytes. In order to indicate its server status the address must invoke listen after bounding to connection-style socket. Its first argument is the socket file descriptor. The number of pending connections which are queued can be known from the second argument. Rejection of additional connections will be done for full queue. This does not limit the total number of connections that a server can handle; it limits just the number of clients attempting to connect that have not yet been accepted.
A server accepts connection requests from a client by invoking accept. The first argument is the socket file descriptor. The second argument points to a socket address structure, which is filled with the client socket’s address. The third argument is the length, in bytes, of the socket address structure. The server can use the client address to determine whether it really wants to communicate with the client. The call to accept creates a new socket for communicating with the client and returns the corresponding file descriptor. The original server socket continues to accept new client connections. To read data from a socket without removing it form the input queue, use recv. It takes the same arguments as read, plus an additional FLAGS argument. A flag of MSG_PEEK causes data to be read but not removed from the input queue.
Local Sockets
Sockets connecting processes on the same computer can use the local namespace represented by the synonyms PF_LOCAL and PF_UNIX. These are called local sockets or UNIX-domain sockets. Their socket addreses, specified by filenames, are used only when creating connections.
The socket’s name is specified in struct sockaddr_un. You must set the sun_family field to AF_LOCAL, indicating that this is a local namespace. The sun_path field specifies the filename to use and may be, at most, 108 bytes long. The actual length of struct sockaddr_un should be computed using the SUN_LEN macro. Any filename can be used, but the process must have directory write permissions, which permits adding files to the directory. To connect to a socket, a process must have read permission for the files. Even though different computers may share the same filesystems, only processes running on the same computer can communicate with local namespace sockets.
The only permissible protocol for the local namespace is 0.
Because it resides in a file system, a local socket is listed as a file. For example, notice the initial s:
% ls –l /tmp/socket
srwxrwx- - x 1 user group 0 Nov 13 19:18 /tmp/socket
Call unlink to remove a local socket when you are done with it.
|