-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
41 lines (30 loc) · 1.34 KB
/
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
from socket import socket, gethostbyname, AF_INET, SOCK_STREAM, gethostname, SOCK_DGRAM
import pickle
import sys
#THIS FILE SHOULD BE RUN WHEN MULTIPLAYER IS DESIRED. ONLY ONE PLAYER NEEDS TO RUN THIS FILE TO HOST MULTIPLAYER
#Written based off of Documentation of Sockets Example 18.1.15 https://docs.python.org/3/library/socket.html#timeouts-and-the-accept-method
clients = {'player1':None,'player2':None}
PORT_NUMBER = 5009
SIZE = 4096
hostName = gethostbyname( gethostname() )
socket = socket( AF_INET, SOCK_DGRAM )
socket.bind( (hostName, PORT_NUMBER) )
print ("Test server listening on port %s\n at %s\n\n" % (str(PORT_NUMBER), str(gethostbyname( gethostname() ))) )
while True:
incoming = socket.recvfrom(SIZE)
(data,addr) = incoming
if pickle.loads(data) == '':
if clients.get('player2', None) == None:
if clients.get('player1',None) != None:
clients['player2'] = addr
print('Player 2 Connected')
else:
clients['player1'] = addr
print('Player 1 Connected')
if incoming[1] == clients['player1']:
if clients['player2']!=None:
socket.sendto(data,clients['player2'])
if incoming[1] == clients['player2']:
if clients['player1']!=None:
socket.sendto(data,clients['player1'])
sys.exit()