Sockets are one of the versatile IPC mechanisms in Unix. Sockets allow point-to-point, two-way communication between processes. Unix sockets are similar to two-way FIFOs. When processes communicate using sockets, data communication occurs using the Unix socket interface. The socket interface is the de-facto standard for communication protocols using sockets. Sockets are designed to be used with different protocols, such as Transmission Control Protocol/Internet Protocol (TCP/IP).
A socket is the endpoint of communication. Each socket has a unique name, a type, and one or more associated processes. Sockets exist in communication domains. A socket domain is an abstraction containing an addressing structure and a set of protocols.
Sockets can be connected with other sockets that belong to the same domain. All the prototypes and definitions related to sockets are provided in the <sys/socket.h> system header file. Of the twenty-three socket domains, only the Unix and Internet domains are used normally. The Unix socket domain can be used to communicate with processes on the same Unix system. The Unix domain provides a socket address space on a single system. Unix domain sockets are named with Unix paths. The syntax of a Unix socket struct is:
struct sockaddr_un
{
unsigned short sun_family; /* AF_UNIX */
char sun_path[108];
}
Creating and Naming a Socket
You can create a socket by calling the socket() system function. The syntax of the function is:
int socket(int domain, int type, int protocol)
int bind(int s, const struct sockaddr *name, int namelen)
#include <sys/socket.h> bind (sd, (struct sockaddr *) &addr, length);
#include <sys/un.h> bind (sd, (struct sockaddr_un *) &addr, length);
#include <netinet/in.h> bind (sd, (struct sockaddr_in *) &addr, length);
Socket Types
The socket type determines the socket communication properties, such as reliability, sequencing, and preventing duplication of messages. The basic socket types defined in the <sys/socket.h> system header file are:
- Stream socket: Provides two-way, sequential, reliable, and unduplicated flow of data without any record boundaries. The socket type is SOCK_STREAM.
- Datagram socket: Allows a two-way flow of messages. The sequence of the data on the datagram socket is not guaranteed. The record boundaries in the data are preserved. The socket type is SOCK_DGRAM.
- Sequential packet socket: Provides a two-way, sequential, and reliable connection for datagrams of a fixed maximum length. The socket type is SOCK_SEQPACKET.
- Raw socket: It provides access to the new communication protocols.
Application of Sockets
Sockets communicate with each other using one of these communication models:
- Client-server model: Where a client process sends a request and the server process listens and services this request
- Peer-to-peer networking model: Where the same process can act as a server process or a client process
- Remote procedure calls (RPC): Where a process is called by the a remote application as a procedure
Processes in a client-server model communicate using sockets. To begin the communication, a socket for the client is connected to a socket for the server. Before the communication can occur, both the client and the server have to be designed. Designing a server includes:
- Creating the socket.
- Naming the socket.
- Attaching a connection to the socket.
- Transferring data using the socket.
- Deallocating the socket after use.
Designing a client includes:
- Creating the socket.
- Attaching a connection.
- Transferring data using the socket.
- Deallocating the socket after use.
Note: It is not always necessary to assign a name to a socket for the client.
Comparison of Sockets for Different Operating Systems
The key differences between the sockets implemented in Unix and those implemented in Windows are:
The header files required by Unix sockets include:
#include <stdio.h>
#include <sys/socket.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include <stdio.h>
#include <winsock.h>
#include<stdlib.h>
The communication part of the sockets is identical in both Unix and Windows OSs.
The shutdown of an application in Unix and Windows is different. Unix uses the close() system function to close a socket. Windows uses the closesocket() system function to close a socket. In addition, Windows use a different error-reporting facility than the one used in Unix.


Currently Active Users
Categories
Latest Blog Entries

Rate this article