Chat Server using Socket Programming

Himanshi Kabra
7 min readJun 20, 2021

Sockets

A socket is one endpoint of a two way communication link between two programs running on the network. The socket mechanism provides a means of inter-process communication (IPC) by establishing named contact points between which the communication take place.

Like ‘Pipe’ is used to create pipes and sockets is created using ‘socket’ system call. The socket provides bidirectional FIFO Communication facility over the network. A socket connecting to the network is created at each end of the communication. Each socket has a specific address. This address is composed of an IP address and a port number.

What is a Server?

Server is a piece of computer hardware or software (computer program) that provides functionality for other programs or devices, called “clients”. This architecture is called the client–server model. Servers can provide various functionalities, often called “services”, such as sharing data or resources among multiple clients, or performing computation for a client. A single server can serve multiple clients, and a single client can use multiple servers. A client process may run on the same device or may connect over a network to a server on a different device.

What is a Client?

In computing, a client is a piece of computer hardware or software that accesses a service made available by a server as part of the client–server model of computer networks. The server is often (but not always) on another computer system, in which case the client accesses the service by way of a network.

Task Overview

👉 Create your own Chat Servers and establish a network to transfer data using Socket Programing by creating both Server and Client machine as Sender and Receiver both. Do this program using UDP data transfer protocol.

👉 Use multi-threading concept to get and receive data parallelly from both the Server Sides. Observe the challenges that you face to achieve this using UDP.

To achieve Socket Programming in Python, we will need to import the socket module. This module consists of built-in methods that are required for creating sockets and help them associate with each other.

Common Methods

Domain

Domain is the family of protocols that is used as the transport mechanism. These values are constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.

Type

Type means the kind of communication between two endpoints, typically SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for connectionless protocols.

Protocol

This may be used to identify a variant of a protocol within a domain and type. Its default value is 0. This is usually left out.

Hostname

This works as the identifier of a network interface. A hostname nay be a string, a dotted-quad address, or an IPV6 address in colon (and possibly dot) notation.

Port

Each server listens for clients calling on one or more ports. A port may be a Fixnum port number, a string containing a port number, or the name of a service.

Python’s Socket Module for Socket Programming

To implement socket programming in python, we need to use the Socket module. Following is a simple syntax to create a Socket −

import socket
s = socket.socket (socket_family, socket_type, protocol = 0)

Here, we need to import the socket library and then make a simple socket. Following are the different parameters used while making socket −

  • socket_family − This is either AF_UNIX or AF_INET, as explained earlier.
  • socket_type − This is either SOCK_STREAM or SOCK_DGRAM.
  • protocol − This is usually left out, defaulting to 0.

Socket Methods

In this section, we will learn about the different socket methods. The three different set of socket methods are described below −

  • Server Socket Methods
  • Client Socket Methods
  • General Socket Methods

Server Socket Methods

In the client-server architecture, there is one centralized server that provides service and many clients receive service from that centralized server. The clients also do the request to server. A few important server socket methods in this architecture are as follows −

  • socket.bind() − This method binds the address (hostname, port number) to the socket.
  • socket.listen() − This method basically listens to the connections made to the socket. It starts TCP listener. Backlog is an argument of this method which specifies the maximum number of queued connections. Its minimum value is 0 and maximum value is 5.
  • socket.accept() − This will accept TCP client connection. The pair (conn, address) is the return value pair of this method. Here, conn is a new socket object used to send and receive data on the connection and address is the address bound to the socket. Before using this method, the socket.bind() and socket.listen() method must be used.

Client Socket Methods

The client in the client-server architecture requests the server and receives services from the server. For this, there is only one method dedicated for clients −

  • socket.connect(address) − this method actively intimate server connection or in simple words this method connects the client to the server. The argument address represents the address of the server.

General Socket Methods

Other than client and server socket methods, there are some general socket methods, which are very useful in socket programming. The general socket methods are as follows −

  • socket.recv(bufsize) − As name implies, this method receives the TCP message from socket. The argument bufsize stands for buffer size and defines the maximum data this method can receive at any one time.
  • socket.send(bytes) − This method is used to send data to the socket which is connected to the remote machine. The argument bytes will gives the number of bytes sent to the socket.
  • socket.recvfrom(data, address) − This method receives data from the socket. Two pair (data, address) value is returned by this method. Data defines the received data and address specifies the address of socket sending the data.
  • socket.sendto(data, address) − As name implies, this method is used to send data from the socket. Two pair (data, address) value is returned by this method. Data defines the number of bytes sent and address specifies the address of the remote machine.
  • socket.close() − This method will close the socket.
  • socket.gethostname() − This method will return the name of the host.
  • socket.sendall(data) − This method sends all the data to the socket which is connected to a remote machine. It will carelessly transfers the data until an error occurs and if it happens then it uses socket.close() method to close the socket.

Features of TCP

· Delivery Acknowledgements

· Retransmission

· Delays transmission when the network is congested

· Easy Error detection

Features of UDP

· Supports bandwidth-intensive applications that tolerate packet loss

· Less delay

· It sends the bulk quantity of packets.

· Possibility of the Data loss

· Allows small transaction (DNS lookup)

UDP Disadvantages:

· Data corruption is a common occurrence on the Internet, UDP has a primitive form of error detection.

· No compensation for lost packets.

· Packets can arrive out of order.

· No congestion control.

Conclusion: UDP may be lightweight, but not that reliable.

For the practical, I would be using two systems. One is Windows 10 and the other is RHEL-8.

Code

MultiThreading

A thread is an entity within a process that can be scheduled for execution. Also, it is the smallest unit of processing that can be performed in an OS. In simple words, it is a sequence of such instructions within a program that can be executed independently of other code. Multithreading is defined as the ability of a processor to execute multiple threads concurrently.

In our chat application, every machine acts as both the client and the server. Hence, the functions send and receive must be executed concurrently. Multithreading must be used to achieve this.

To create a new thread, we create an object of the Thread class. It takes the following arguments:

· target: the function to be executed by the thread

· args: the arguments to be passed to the target function

Eg: t1 = Thread( target = receive , args = (myIP,myPort))

Once a thread object is created, its activity must be started by calling the thread’s start() method. This invokes the run() method in a separate thread of control.

Conclusion: UDP may be lightweight, but not that reliable.

--

--