Skip to content

aalianahmad/Python_tcp_server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

🖥️ Python TCP Client–Server Communication

📘 Overview

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.

⚙️ How It Works

• 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.

🌐 Network Concepts Learned

• 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.

🧩 How to Run

  1. Start the server:
python3 server.py
  1. Start the client in another terminal:
python3 client.py
  1. Enter messages in the client terminal — they will appear on the server. Type "close" to end the connection.

🧠 Learning Outcomes

• 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages