Socket
- The socket mechanism provides a means of inter-process communication (IPC) by establishing named contact points between which the communication take place.
- Thus, a socket is defined as an endpoint for communication.
- Two processes that are two different computer system (connected via network) can exchange messages using socket.
- Like pipe is used to create pipes, socket is created using socket system call.
- We can also say that socket provides bidirectional FIFO communication facility over the network.
- A pair of processes communicating over a network employs a pair of socket- one for each process.
Thus, a socket connecting to the network is created at each end of the communication and when sockets are inter-connected a dialogue can take place.
Socket Address
- Each socket has a specific address. This address is composed of an IP address and a port number.
- The nature of the address depends on the communication domain (i.e. UNIX domain. the Internet domain, etc.) of the socket.
- It is the property of domain that a number of processes communicating in the same domain use the same property of a domain that a number of processes communicating in the same domain use the same address format.
Types of Sockets
There are two types of socket the datagram socket and the stream socket.
- The datagram socket is analogous to mailbox. The letters (data) posted into the box are collected and delivered (transmitted) to a letterbox (receiving socket).
- The stream socket is analogous to a telephone. A connection is established between the phones (two ends) and a conversation (transfer of data) takes place.
- Stream socket is supported in the Internet domain by the TCP protocol Datagram socket transfer messages of variable size in either director by the UDP protocol.
Application of Socket
- Socket are generally employed in client-server applications.
- The server creates a socket, attaches it to a network port address, then waits for a client to contact it.
- The client creates a socket and then attempts to connect to the server socket. When the connection is established, transfer of data takes place.
- Servers implementing specific services (such as telnet, ftp and http) listen to well-known ports (a telnet server listens to port 23, an FTP server listens to port 21 and a web (or http) server listens to port 80). All ports below 1024 are considered well known.
- When a client process initiates a request for a connection, it is assigned a port by the host computer. This port is some arbitrary number greater than 1024. For example, it a client on host X with IP address 146.865.20 wishes to establish connection with a web server (which is listening on port 80) at address 161.25.19.8, host X may be assigned port 1625.
- Thus, a connection will consist of a pair of sockets (146.86.5.20: 1625) on host X and (161.25.19.8: 80) on the web server.
- The packet travelling between the hosts are delivered to appropriate process, based on the destination port number.
- All connections must be unique. Therefore, if another process also on host X want to establish another connection with the same web server, it has to be assigned another port number (greater than 1024) and not 1625. This ensures that all connections consist of unique pair of sockets.
Implementation of Socket and Various system calls
The various system calls and their purpose used in implementing sockets are given below:
Function Call | Description |
create( ) | To create a socket |
bind( ) | Its socket identification like a telephone number to contact |
listen( ) | Ready to receive a connection |
connect( ) | Ready to act as a sender |
accept( ) | Confirmation, it is like accepting to receive a call from a sender |
write( ) | To send data |
read( ) | To receive data |
close( ) | To close a connection |
The following procedure is used to set up a socket:
1. A socket is created using socket() system call. It takes as arguments the specification of communication domain, the socket type and the protocol to be used. The value returned is a socket descriptor having the same name as file descriptors.
The socket descriptor points to the array of open files in the structure in the kernel and has a file structure allocated to it.
2. For another process to use the socket, it is addressed by name. A name is attached to socket by the bind( )system call.
The bind() system call takes the socket descriptor pointer to the name and the length of the name as a byte string. The contents and the length of the byte string depend on the address format.
3. The connection to the socket is initiated using connect() system call. This call uses same arguments or parameters as that of bind( ) system call. Always the socket descriptor represents the local socket and the address is that of the foreign socket to which the attempt to connect is made.
4. After creating a socket (using socket() system call) and binding the well-known address of its service to that socket (using bind() system call), the sever tells the kernel that it is ready to accept the connections from the client. This is done by a server using listen( ) system call. This call takes socket descriptor of original socket as an argument.
5. Finally, the server uses accept( ) system call to accept the individual connections. It also takes socket descriptor of the original socket as an argument. Accept( ) system call returns a new socket descriptor corresponding to the new connection. The original socket descriptor is still open for further connections.
6. Next the server uses fork( ) system call to produce a new process after the ‘accept‘ to service the client while the original parent server process continues to listen for more connections
7. Once a connection for a socket type (eg. stream socket) is established, the address of both the endpoints are known. In such a situation read( ) and write( ) system calls may be used to transfer data between two endpoints
8. In order to terminate a connection and destroy the associated socket, close() system call is used. One can also terminate only one direction of communication of a duplex connection using shutdown( )system call.
- Some socket types, such as datagram socket, do not support connections. They make use of ‘sendto‘ and ‘recvfrom‘ system calls.
- These system calls take as arguments a socket descriptor, a buffer pointer and the length and an address buffer pointer and length.
- The address buffer contains the address to send to for ‘send to’ and is filled with the address of the datagram just received from ‘recvfrom‘.
- Thus, in order to implement sockets, the server does a ‘socket‘ ‘bind‘ and ‘listen‘ for each service and then does a ‘select‘ on all the socket descriptors. When ‘select‘ indicates activity on a descriptor, the server does an ‘accept’ on it and forks a process on the new descriptor returned by ‘accept‘.