Building a Simple TCP Server
A TCP server is a program that listens for incoming network connections, receives data from clients, and can send responses back.
You should use a TCP server when you need a reliable service, for example a chat server, file server, or web server. TCP guarantees that data arrives in the correct order and without loss, which makes it suitable for applications where consistency and accuracy matter. TCP servers are often used when you want full control over how clients connect and communicate with your system.
1234567891011121314151617181920212223import socket # Create a TCP/IP socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to localhost on port 65432 server_socket.bind(("127.0.0.1", 65432)) # Listen for incoming connections (allow 1 connection in the queue) server_socket.listen(1) print("Server is listening on 127.0.0.1:65432") # Wait for a connection connection, client_address = server_socket.accept() print("Connection from", client_address) # Receive data in small chunks data = connection.recv(1024) print("Received:", data.decode()) # Clean up the connection connection.close() server_socket.close()
This server code begins by importing the socket module, which is Pythonβs standard way to work with network sockets. The server creates a new socket using socket.socket(socket.AF_INET, socket.SOCK_STREAM). Here, AF_INET tells Python to use IPv4 addresses, and SOCK_STREAM sets up a TCP connection. The server then binds to the address 127.0.0.1 (localhost) and port 65432, making it available for clients that connect to this port on the same machine.
Next, the server starts listening for incoming connections by calling listen(1). The number 1 means it will allow only one connection to wait in the queue at a time. When a client connects, accept() returns a new connection object and the address of the client. The server prints out the clientβs address to confirm the connection. It then waits to receive up to 1024 bytes of data from the client using recv(1024). The received data is decoded from bytes to a string for easier reading and printed to the console.
Once the data is received, the server closes both the connection to the client and the main server socket. This simple server handles a single connection and then shuts down, which is useful for basic testing and understanding how TCP servers work in Python.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 9.09
Building a Simple TCP Server
Swipe to show menu
A TCP server is a program that listens for incoming network connections, receives data from clients, and can send responses back.
You should use a TCP server when you need a reliable service, for example a chat server, file server, or web server. TCP guarantees that data arrives in the correct order and without loss, which makes it suitable for applications where consistency and accuracy matter. TCP servers are often used when you want full control over how clients connect and communicate with your system.
1234567891011121314151617181920212223import socket # Create a TCP/IP socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to localhost on port 65432 server_socket.bind(("127.0.0.1", 65432)) # Listen for incoming connections (allow 1 connection in the queue) server_socket.listen(1) print("Server is listening on 127.0.0.1:65432") # Wait for a connection connection, client_address = server_socket.accept() print("Connection from", client_address) # Receive data in small chunks data = connection.recv(1024) print("Received:", data.decode()) # Clean up the connection connection.close() server_socket.close()
This server code begins by importing the socket module, which is Pythonβs standard way to work with network sockets. The server creates a new socket using socket.socket(socket.AF_INET, socket.SOCK_STREAM). Here, AF_INET tells Python to use IPv4 addresses, and SOCK_STREAM sets up a TCP connection. The server then binds to the address 127.0.0.1 (localhost) and port 65432, making it available for clients that connect to this port on the same machine.
Next, the server starts listening for incoming connections by calling listen(1). The number 1 means it will allow only one connection to wait in the queue at a time. When a client connects, accept() returns a new connection object and the address of the client. The server prints out the clientβs address to confirm the connection. It then waits to receive up to 1024 bytes of data from the client using recv(1024). The received data is decoded from bytes to a string for easier reading and printed to the console.
Once the data is received, the server closes both the connection to the client and the main server socket. This simple server handles a single connection and then shuts down, which is useful for basic testing and understanding how TCP servers work in Python.
Thanks for your feedback!