How to create a simple Python TCP/IP Server and Client?
IPC is a communication mechanism that an Operating System offers for processes to communicate with each other. There are various types of IPCs such as:
- Pipes
- Sockets
- Files
- Signals
- Shared Memory
- Message Queues/ Message Passing
Sockets
Sockets are used to send data over the network either to a different process on the same computer or to another computer on the network.
There are four types of sockets namely,
- Stream Sockets
- Datagram Sockets
- Raw Sockets
- Sequenced Packet Sockets
Stream sockets and datagram sockets are the two most popular choices.
Stream Sockets | Datagram Sockets |
Guaranteed delivery | No delivery guarantees |
Uses TCP (Transmission Control Protocol) | Used UDP (User Datagram Protocol) |
Needs an open connection | Don’t need to have an open connection |
How are sockets used in Distributed Systems?
Distributed Systems are built using the concept of Client Service architectures.
- Clients send requests to servers
- Servers send back responses or error codes accordingly
The communication across servers and clients in a distributed system uses sockets as a popular form of IPC. Sockets are nothing but a combination of
Each machine (with an IP address) has several applications running on it. We need to know on which port an application is running in to send requests to it.
What is TCP/IP?
We will go into the details of communication protocols in a different article and stick to the basics for today. TCP stands for Transmission Control Protocol, a communications protocol for computers to exchange information over a network.
IP stands for Internet Protocol. IP identifies the IP address of the applications or devices to send data to and forms the Network Layer in the OSI stack. TCP defines how to transport the data over the network. Ensuring delivery guarantee is still TCP’s job.
When we send an HTTP request to a server, we first establish a TCP connection, so HTTP sits on top of TCP as the transport layer. When a user types a URL into the browser, the browser sets up a TCP socket using the IP address and port number and starts sending data to that socket. This request is sent as bytes in the form of data packets over the network. The server will then respond to the request. The benefits of a TCP connection is that a server sends acknowledgement of each packet based on which the client retransmits data in case some packets get dropped. Each packet has a sequence number that the server uses to assemble them upon receiving.
Now let’s look at an example Python program on how to write a simple script to setup a TCP/IP server and client.
Python TCP/IP server
import socket # Set up a TCP/IP server tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to server address and port 81 server_address = ('localhost', 81) tcp_socket.bind(server_address) # Listen on port 81 tcp_socket.listen(1) while True: print("Waiting for connection") connection, client = tcp_socket.accept() try: print("Connected to client IP: <>".format(client)) # Receive and print data 32 bytes at a time, as long as the client is sending something while True: data = connection.recv(32) print("Received data: <>".format(data)) if not data: break finally: connection.close()
Python TCP/IP Client
import socket # Create a connection to the server application on port 81 tcp_socket = socket.create_connection(('localhost', 81)) try: data = str.encode(‘Hi. I am a TCP client sending data to the server’) tcp_socket.sendall(data) finally: print("Closing socket") tcp_socket.close()
Terminal Output
Waiting for connection Connected to client IP: ('127.0.0.1', 65483) Received data: Hi. I am a TCP c Received data: lient sending da Received data: ta to the server Received data: Waiting for connection
To find and kill any applications running on a port.
List the processes running on port 81
Get the PID number and kill the process
Hope you enjoyed learning how to setup a simple TCP/IP server and client using Python.