Socket Programming
Socket:
Sockets are basically used for inter process communication within single or over the network systems. So sockets are basically two endpoints of the communication via which we can communicate. So socket represents a single connection between exactly two pieces of software but we can also communicate between multiple piece of softwares but this requires multiple sockets.
*Sockets are use of unidirectional as well as bidirectional communication.
Now as we know if we have to work on the network then we have to deal with IP addresses also because each and every system on the network is recognized by its IP address.In this we have one server and many clients to take the service of the server. Notice that the client needs to know the existence of and the address of the server, but the server does not need to know the address of (or even the existence of) the client prior to the connection being established. Notice also that once a connection is established, both sides can send and receive information.
Socket Types:
There are two widely used socket types, stream sockets, and datagram sockets. Stream sockets treat communications as a continuous stream of characters, while datagram sockets have to read entire messages at once. Each uses its own communications protocol. Stream sockets use TCP (Transmission Control Protocol), which is a reliable, stream oriented protocol, and datagram sockets use UDP (Unix Datagram Protocol), which is unreliable and message oriented.
The address of a socket in the Internet domain consists of the Internet address of the host machine (a unique 32 bit address, often referred to as its IP address). In addition, each socket needs a port number on that host. Port numbers are 16 bit unsigned integers. The lower numbers are reserved in Unix for standard services. For example, the port number for the FTP server is 21. It is important that standard services be at the same port on all computers so that clients will know their addresses. However, port numbers above 2000 are generally available.
API to establish socket:
In server side:
Socket system calls are declaired in the linux system at standard path in the <sys/socket.h> header file.
Firstly we have to make socket with socket() call. When a socket is created, the program has to specify the address domain and the socket type. Two processes can communicate with each other only if their sockets are of the same type and in the same domain. There are two widely used address domains, the unix domain, in which two processes which share a common file system communicate, and the Internet domain, in which two processes running on any two hosts on the Internet communicate. Each of these has its own address format.
Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
Listen for connections with the listen() system call
Accept a connection with the accept() system call. This call typically blocks until a client connects with the server.
In client side:
In the client side also we have to make socket,Connect the socket to the address of the server using the connect() system call
Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.
We can check our client and server program by loopback means by giving the IP address “127.0.0.1”.
on client and server side on single system.
Thanks
Avtar Singh