0% found this document useful (0 votes)
13 views2 pages

TCP Concurrent Server

The document contains a Python script that implements a simple multi-threaded TCP server. It listens for incoming client connections, echoes received data back to the clients, and handles connection errors gracefully. The server runs indefinitely, accepting and processing multiple clients concurrently using threads.

Uploaded by

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

TCP Concurrent Server

The document contains a Python script that implements a simple multi-threaded TCP server. It listens for incoming client connections, echoes received data back to the clients, and handles connection errors gracefully. The server runs indefinitely, accepting and processing multiple clients concurrently using threads.

Uploaded by

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

import socket

import threading

def handle_client(client_socket, address):

print(f"[+] New connection from {address}")

try:

while True:

data = client_socket.recv(1024)

if not data:

break

print(f"[Received] {data.decode()} from {address}")

client_socket.sendall(data) # Echo the received data

except ConnectionResetError:

print(f"[-] Connection lost with {address}")

finally:

client_socket.close()

print(f"[-] Connection closed with {address}")

def start_server(host='0.0.0.0', port=5555):

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server.bind((host, port))

server.listen(5)

print(f"[*] Listening on {host}:{port}")

while True:
client_socket, addr = server.accept()

client_handler = threading.Thread(target=handle_client, args=(client_socket, addr))

client_handler.start()

if __name__ == "__main__":

start_server()

You might also like