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

Server

The document contains a Python script for a simple chat server that handles multiple clients using sockets and threading. It allows clients to connect, send messages, and broadcasts those messages to other clients while managing client connections and disconnections. The server runs on a specified host and port, and it can be shut down gracefully with a keyboard interrupt.

Uploaded by

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

Server

The document contains a Python script for a simple chat server that handles multiple clients using sockets and threading. It allows clients to connect, send messages, and broadcasts those messages to other clients while managing client connections and disconnections. The server runs on a specified host and port, and it can be shut down gracefully with a keyboard interrupt.

Uploaded by

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

import socket

import threading

clients = []
client_names = {}

def broadcast(message, sender_socket):


for client in clients:
if client != sender_socket:
try:
client.sendall(message.encode())
except:
clients.remove(client)

def handle_client(client_socket, address):


print(f"New connection from {address}")
client_socket.send("Enter your name: ".encode())
name = client_socket.recv(1024).decode().strip()
welcome = f"{name} joined the chat!"
print(welcome)
broadcast(welcome, client_socket)

clients.append(client_socket)
client_names[client_socket] = name

try:
while True:
data = client_socket.recv(1024).decode()
if not data or data.lower() == 'exit':
break

message = f"{name}: {data}"


print(message)
broadcast(message, client_socket)
except:
pass
finally:
print(f"{name} disconnected.")
clients.remove(client_socket)
broadcast(f"{name} left the chat.", client_socket)
client_socket.close()
del client_names[client_socket]

def start_server(host='192.168.1.4', port=65432):


server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen()
print(f"Server running on {host}:{port}")

try:
while True:
client_socket, address = server_socket.accept()
threading.Thread(target=handle_client, args=(client_socket, address),
daemon=True).start()
except KeyboardInterrupt:
print("\nServer shutting down.")
finally:
for client in clients:
client.close()
server_socket.close()

if __name__ == '__main__':
start_server()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy