-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
37 lines (32 loc) · 1.13 KB
/
server.py
File metadata and controls
37 lines (32 loc) · 1.13 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
import socket
import threading
from server_connection import Client
from game import Game
from snake import Snake
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Initializing socket
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serverSocket.bind(("127.0.0.1", 3141))
serverSocket.listen(4)
print("Server started on port 3141 ...")
print("Listening for connections ...")
clients = []
while True:
# Listening for connections
clientSocketOutputStream, address = serverSocket.accept()
print("New Connection:", clientSocketOutputStream.getpeername()[1])
snake = Snake(len(clients) + 1)
client = Client(clientSocketOutputStream, snake)
thread = threading.Thread(target=client.runClient)
thread.start()
clients.append(client)
# if all 4 players have joined stop accepting new connections
if len(clients) == 2:
print("All players joined!")
break
# Initializing game
game = Game(clients)
game.run_game()
except KeyboardInterrupt:
serverSocket.close()