0% found this document useful (0 votes)
27 views1 page

TCP Echo Client Server

The document provides a Python implementation of a TCP-based Echo Client and Server. The server listens for connections, receives data, and echoes it back to the client, while the client sends messages to the server and displays the echoed responses. Both components use socket programming to establish communication over a specified host and port.

Uploaded by

Kathan Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views1 page

TCP Echo Client Server

The document provides a Python implementation of a TCP-based Echo Client and Server. The server listens for connections, receives data, and echoes it back to the client, while the client sends messages to the server and displays the echoed responses. Both components use socket programming to establish communication over a specified host and port.

Uploaded by

Kathan Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

TCP-based Echo Client-Server Program in Python

TCP Echo Server:


import socket

def start_server(host='127.0.0.1', port=12345):


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
server_socket.bind((host, port))
server_socket.listen(1)
print(f"Server listening on {host}:{port}")

conn, addr = server_socket.accept()


with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(1024)
if not data:
break
print(f"Received: {data.decode()}")
conn.sendall(data) # Echo the data back to the client

if __name__ == "__main__":
start_server()

TCP Echo Client:


import socket

def start_client(host='127.0.0.1', port=12345):


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
client_socket.connect((host, port))
print(f"Connected to server at {host}:{port}")

while True:
msg = input("Enter message (or 'exit' to quit): ")
if msg.lower() == 'exit':
break

client_socket.sendall(msg.encode())
data = client_socket.recv(1024)
print(f"Echo from server: {data.decode()}")

if __name__ == "__main__":
start_client()

You might also like