TCP Client-Server Network using Socket Programming Python

Abhinav Dubey
4 min readJun 30, 2021

This article covers a brief introduction to the TCP network and the tutorial of a simple implementation of TCP/IP Client-Server Network Connection using the Socket library of Python, Socket is a standard python library for low-level networking interface and contains various built-in methods.

A network socket object act as the endpoint of communication channels for sending and receiving data across the network. Each TCP connection has a 16-bit unsigned port number (0–65535) reserved. Transmission Control Protocol (TCP) uses port numbers to identify the endpoints of applications for sending and receiving which are called internet sockets.

When we use the browser to open websites, we are essentially opening a TCP connection from our computer to the website. Our local computer uses the IP address and the port number to identify itself to the website. HTTP is used to transfer the web pages from the server to our browsers, using TCP on port 80.

https://www.inetdaemon.com/

In the case of TCP, the connection has to be established before sending the data packets. It works on a three-way handshake practice (SYN, SYN-ACK, ACK). Any protocol working on the top of TCP also performs a three-way handshake.

  1. The client sends a segment with SYN ( Synchronize Sequence Number) to the server in the first step.
  2. The server responds with an SYN-ACK message. The header of the TCP packet will have both the SYN and bit turned on. Acknowledgment (ACK) here means that the server has received the client’s request.
  3. In the final step, the client acknowledges the response from the server and sends an ACK to the server, and establishes a connection for the data packets to be sent or received.

When the communication is ended between two hosts, again a three-way handshake is performed. This three-way handshake introduces latency but also the reliability of the connection.

So, we are going to establish a TCP connection with a help of the Socket library of python on our local system.

We will need two python files, one for the server and the other for the client.

Let’s start with the server first.

Server

Import the socket library, then define our TCP port and address.

We will now create our TCP socket object. AF_INET is the address family that designating the address type our socket can communicate with, in this case, IPv4, for IPv6 there is AF_INET6.

SOCK_STREAM is the socket type for TCP network and for UDP Networks socket type is SOCK_DGRAM.

The bind() function binds the socket object with the address, provided that the address is not already bounded.

The listen() function enables a server to accept connections and the argument given determines the number of unaccepted functions before refusing new connections.

The accept() function accepts the connection request made by clients and returns a new socket object, which will be used to send and receive data, and the address of the client.

The server will start receiving data with a buffer size of 30. The message received is UTF-8 encoded to convert string type to bytes, so we have to decode it using the decode() function and then send a return message to the client after encoding it to UTF-8 from string format.

Now, after the communication is all done, simply disconnect and close the returned socket and the server socket.

Client

Similarly for the client, import the socket library, define the TCP port, and address.

Create a socket object with AF_INET address family and SOCK_STREAM socket type.

Now we will connect the client to the TCP port on which our server is running. The connect() function connects the client socket object using the IP address passed as an argument.

After the client has established the connection successfully with the Server, encode and send the data.

Receive the data sent by the server and decode it to string format from UTF-8 encoding.

Just like as the server, after the communication is over, disconnect the client from the server and close the socket connection.

To run both programs concurrently, you can use the ‘&’ symbol with the python command to run the server as a background process or just use two different terminals for the server and client.

python server.py &

python client.py

Get the complete code on my GitHub Account —

--

--

Abhinav Dubey

Ex-R&D Associate, Indian Space Association | Data Scientist | IIT Madras | IIIT Delhi | UnivOfDel | LinkedIn - https://www.linkedin.com/in/abhinav-dubey-007001