-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleserverfull.py
More file actions
89 lines (69 loc) · 2.43 KB
/
singleserverfull.py
File metadata and controls
89 lines (69 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import socket
import threading
from datetime import datetime
# Server configuration
HOST = '127.0.0.1'
PORT = 12345
def timestamp():
"""
Returns the current time formatted as HH:MM:SS.
Used to tag messages for easier tracking.
"""
return datetime.now().strftime("%H:%M:%S")
def receive_messages(client_socket):
"""
Thread function to continuously listen for messages from the client. Runs in the background, allowing the main thread to handle sending.
"""
while True:
try:
data = client_socket.recv(1024)
if not data:
# Client disconnected cleanly
print(f"\n[{timestamp()}] Client disconnected.")
break
print(f"\n[{timestamp()}] Client: {data.decode()}" )
print("You: ", end="", flush=True)
except Exception as e:
print(f"\n[{timestamp()}] Receive error:", e)
break
def send_messages(client_socket):
"""
Main thread function for server input and sending messages to client. Loops until server types '/quit' or client disconnects.
"""
while True:
try:
message = input("You: ")
if message.lower() in ("/quit", "exit"):
print(f"[{timestamp()}] Closing connection...")
client_socket.send("Server has left the chat.".encode())
break
client_socket.send(message.encode())
except Exception as e:
print(f"\n[{timestamp()}] Send error:", e)
break
def main():
"""
Entry point for the server.
Sets up the socket, accepts a client, and starts threads for communication.
"""
# Create TCP socket (IPv4)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind socket to host and port
server_socket.bind((HOST, PORT))
# Listen for incoming connections (1 means max 1 queued connection)
server_socket.listen(1)
print(f"[{timestamp()}] Server listening on {HOST}:{PORT}...")
client_socket, addr = server_socket.accept()
print(f"[{timestamp()}] Client connected from {addr}")
recv_thread = threading.Thread(
target=receive_messages,
args=(client_socket,),
daemon=True
)
recv_thread.start()
send_messages(client_socket)
client_socket.close()
server_socket.close()
print(f"[{timestamp()}] Server shutdown.")
if __name__ == "__main__":
main()