-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompetition_server.py
79 lines (58 loc) · 2.13 KB
/
competition_server.py
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
import random
import socket
import threading
from brain_brawl.settings import IP
# Select port number
def generate_port_number():
# Generate a random number between 1000 and 9999 (both inclusive)
number = random.randint(1000, 9999)
return number
# IP = socket.gethostbyname(socket.gethostname())
# PORT = generate_port_number()
PORT = generate_port_number()
ADDR = (IP, PORT)
SIZE = 1024
FORMAT = "utf-8"
DISCONNECT_MSG = "!DISCONNECT"
# Counter for connected clients
# client_count = 0
def handle_client(conn, addr):
# global client_count
print(f"[New Player Joined this Window] {addr} connected.")
# # Increment client count and check if it exceeds the limit
# client_count += 1
# if client_count > 2:
# # print(f"[SERVER FULL] {addr} was rejected due to maximum occupancy.")
# # conn.send("Server is full. Try again later.".encode(FORMAT))
# conn.close()
# client_count -= 1 # Decrement client count if rejected
# return
connected = True
while connected:
conn.send("welcome to the BattleField. Finding your worthy contender".encode(FORMAT))
answer = conn.recv(SIZE).decode(FORMAT)
if answer == DISCONNECT_MSG:
connected = False
print(f"[{addr}] Answer: {answer}")
# Decrement client count when client disconnects
# client_count -= 1
conn.close()
def competition_server():
print("[STARTING] Server is starting...")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
server.listen()
print(f"[LISTENING] Server is listening on {IP}:{PORT}")
while True:
# # Check if maximum client count reached
# if client_count >= 2:
# print("[SERVER FULL] Maximum client count reached. Closing server...")
# break
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
# Close the server socket
server.close()
# # Start the competition server in a separate thread
# competition_thread = threading.Thread(target=competition_server)
# competition_thread.start()