This project demonstrates how a client and server communicate using TCP sockets in Python. Both files — server.py and client.py — use the built-in socket library to establish a reliable, connection-oriented channel between two processes.
I built this project to understand how network communication works at a lower level — including IP addresses, ports, endpoints, packets, and TCP handshake.
• The server creates a socket using:
socket.socket(socket.AF_INET, socket.SOCK_STREAM)- AF_INET → means IPv4 addresses are used.
- SOCK_STREAM → means TCP protocol is used (connection-oriented and reliable).
• The server then: a. Binds to an IP and port → creates an endpoint for clients. b. Listens for incoming connections. c. Accepts a client → creating a dedicated socket for communication. d. Receives and sends messages in UTF-8 encoding until the client sends "close".
• The client connects to the server and sends messages in a loop. It encodes messages before sending and decodes responses received from the server.
• IP Address: Identifies a device on the network. • Port: Identifies a specific process (like a "room number" in a hotel). • Endpoint: Combination of IP address + port number. • Packet: Unit of data sent between endpoints. • Loopback Interface (127.0.0.1): Used for communication between processes on the same machine. • TCP Handshake: 3-step process to establish a reliable connection. • Socket States: LISTEN, ESTABLISHED, CLOSE_WAIT, TIME_WAIT.
- Start the server:
python3 server.py- Start the client in another terminal:
python3 client.py- Enter messages in the client terminal — they will appear on the server. Type "close" to end the connection.
• Built understanding of how TCP sockets work internally. • Learned difference between listening socket and connected socket. • Learned to send and receive data using send() and recv(). • Understood how encoding and decoding ensure proper data transfer. • Practiced using f-strings, loops, and conditionals in a real-world program.