Learn how to use sockets in IPC, TCP/IP and Advanced Network Programming.
Well, To be simple, Socket is a way of communication between two things. Like the sockets which are used in the electrical accessories that connect that accessory to the power source, the socket concept in programming and networking easily connect two computers, processes or two other things which are present in computer and networking, which will be covered soon.
Imagine someone who is calling his friend on the phone. The one who is calling is called a client and the one who is going to respond to this action and pick the phone up, is called a server.
Sockets are meant to be used on Client-Server models. By saying "Client Server", I mean any model that is going to setup a communication path between two processes or etc. So the most important usage of sockets are introduced in the Client-Server model as channels in the communication. Notice that the client does need to know the address(phone number :D) of the server but the server doesn't need that for just responding to him prior to that connection. But as soon as the communication is established between them, they both can send and recieve the data and they can grab each other's ip and each other status information.
To communicate, both server and the client need to create a Socket connection on their sides. So the Socket connection is opened by client and the server. You will get familiar of working with these sockets and passing the data through them very soon.
Keep up with the following steps to create a new socket on the client side:
- Create a socket with the socket() system call.
- Connect the socket to the address of the server using the connect() system call.
- Send and recieve data. The simplest way is to use the read() and write() system calls.
- Create a socket with the socket() system call.
- Bind the socket to an address using the bind() system call. This address will consist of the port number for a socket on the internet.
- Listen for connections with the listen() system call.
- Accept a connection with the accept() system call. This call is blocked untill a connection is successfully created.
- Send and recieve data.
There are two critical things that each program must define after creating a new socket. Those are:
- Address Domain
- Socket Type Remember that two processes are able to communicate with each other if they have the same socket types in the same domain.
There are two most important domain types:
- UNIX Domain
- Internet Domain
In the UNIX Domain, two processes share a common file system communication, Since the UNIX is using the file system to share the data between the processes. The Internet Domain is the domain in which two processes runnning on any two hosts on the internet communicate.
Each of the domain types discussed above, has it's own address format.
- The address of a socket in the UNIX domain is a character string which is basically an entry in the file system.
- The address of a socket in the Internet domain consist of IP Address of the host + Port Number on that host.
Every computer on the internet has a unique 32 bit address which is called IP Address. Each port number is a 16 bit unsigned integer which shows the doors through that host or computer.
Two major sockets are:
- Stream Sockets
- Datagram Sockets
Stream sockets treat communications as a continuous stream of characters, while the Datagram sockets have to read the entire messages at once. Each of these sockets has its own communication protocol.
Stream sockets use TCP(Transmission Control Protocol), which is a a reliable and stream oriented protocol. Datagram sockets use UDP(Unix Datagram Protocol), which is unreliable and message oriented.
- sockaddr_in - Connection information. Used by connect , send , recv etc.
- in_addr - Ip address in long format
- sockaddr
- hostent - The ip addresses of a hostname. Used by gethostbyname
The listen system call allows the process to listen on the socket for connections. The first argument is the socket file descriptor, and the second is the size of the backlog queue, i.e., the number of connections that can be waiting while the process is handling a particular connection. This should be set to 5, the maximum size permitted by most systems. If the first argument is a valid socket, this call cannot fail, and so the code doesn't check for errors.
While trying to set up a server to listen for the connections, Its better to set it up as a Live Server, Meaning that it should wait for all of the connections to connect, until its shut down manually. We can do this by keeping the accept system call in a loop until its manually shuts down.
To handle every connection we need a separate handling code to run along with the main server accepting connections. One way to achieve this is using threads. The main server program accepts a connection and creates a new thread to handle communication for the connection, and then the server goes back to accept more connections. On Linux threading can be done with the pthread (posix threads) library. It would be good to read some small tutorial about it if you dont know anything about it. However the usage is not very complicated.