17 June 2020   |   by Jatin Jeena   |   Networking, Blogs

Client Server Program in Python

One to one and one to many client chat, including private message functionality


Making a client server program in Python is now no more a stressing and confusing task. In this article, we will explain you step by step, how to make your own private chatroom by using sockets and client & server functions in Python in a much easy and understandable way.




What are sockets?

Sockets are the endpoints of a bidirectional communication channel. Sockets are used to send and receive data bi-directionally. Sockets may be implemented using different channel types TCP, UDP and so on. Sending and receiving of the data is done is bytes. Therefore encoding and decoding of the data is done before sending and receiving respectively. Python provides SOCKETS module to implement sockets.


Server socket methods

1. socket.bind() – Binds IP address and PORT no. with the socket.
2. socket.listen() – Listens for new connections.
3. socket.accept() – It accepts and waits for an incoming connection when a client connects. It returns new socket object representing the connection and tuple holding address of client tuple(host, port).


Client socket methods

1. socket.connect() – Used by the client socket to connect to the server socket, using the IP address and the port number.


Socket methods for both client & server

1. Socket.recv() – for receiving the TCP message.
2. Socket.send() – for transmitting a TCP message.
3. Socket.recvfrom() – for receiving a UDP message.
4. Socket.sendto() – for transmitting a UDP message.
5. Socket.close() – for closing the socket.
6. Socket.gethostname() – it returns the name of the host.


We will be creating two files server.py and client.py. Let’s look and understand the working of each file in detail.

Server.py code

Step by step implementation

1. An IP address and PORT will be set for the server. The PORT should be set wisely, the selected PORT should not conflict with already selected PORT for any service.

2. Creating a socket, S = socket.socket(socket_famiy, socket_type)
This line creates the socket object. Socket family in the above code specify the address family that will be used in the communication. AF_NET is used for internet address family IPV4. Socket_type in the code tells the type of connection. SOCK_SREAM is for TCP connection and SOCK_DGRAM is used for UDP connection.

3. Then we will bind the socket object with the selected IP address and the PORT.

4. We will listen for the incoming connection using listen() function, which receives an integer value as argument. The one and only argument in the listen() specifies the number of unaccepted connections that the system will allow before refusing new connection.

5. We will create a list named “sockets_list” and a dictionary named “clients” to store the details related to the clients, such as their address and name of each client etc.

6. We will also create two functions “receive_message” and “look_for_messages”.

7. The reveive_message function will receive the message using the recv() function.

8. The look_for_messages function will continuously check if any client has sent the new connection request or any new message. It does so using the select() function of the select module. The select module provides access to platform specific I/O monitoring functions. select() deals with sockets, openfiles and pipes until they become readable and writable. The select() function has 3 arguments which are listed below.

a. List of objects to be checked for incoming data to be read.
b. List of object that will receive outgoing data.
c. List of objects that may be give error.

This method then waits until any socket becomes readable or writable. If any socket becomes readable then it call reveive_message function to receive message.

9. This function also does the task of accepting the new connections form the client as well as it also receives the messages from the already connected client.

10. Now as we have to look for any message and also send the messages simultaneously so we will use threading module to do both the tasks together.

11. While loop is used to continuously send the messages.


Output of Server.py
Client.py code

Step by step implementation

1. We will create the client socket using the socket.socket() function.

2. We will connect the client socket to the server socket using the connect() function. The connect() takes the IP address and the PORT number of the server as argument. Connect() opens a TCP connection on the specified Address and the PORT.

3. Then we will bind the socket object with the selected IP address and the PORT.

4. And send() method is used to send the messages.

5. Here again we have to perform the two tasks of sending and receiving the messages simultaneously so we will use the threads to achieve this functionality.


Output of Client.py