-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.py
92 lines (73 loc) · 1.78 KB
/
client.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
80
81
82
83
84
85
86
87
88
89
90
91
92
# Client protocol for battleship
import socket
import pickle
# def try_connect(ip):
# HOST = ip
# PORT = 58008
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# try:
# s.connect((HOST, PORT))
# s.close()
# except:
# print ("oh noes")
# return False
# return True
def get_preamble(s):
debug = False
if debug:
print ('Getting preamble...')
ePreamble = s.recv(1024)
preamble = ePreamble.decode()
s.sendall(ePreamble)
if debug:
print ('Recieved!')
return preamble
def get_boards(s):
debug = False
gameboards = []
for x in range(0,4):
if debug:
print ('Getting board...')
pData = s.recv(1024)
data = pickle.loads(pData)
s.send(pData)
if debug:
print ('Recieved!')
gameboards.append(data)
return gameboards
def send_boards(s, gameboards):
debug = False
try:
for board in gameboards:
pData = pickle.dumps(board)
if debug:
print("Sending data...")
s.send(pData)
pDataRecv = s.recv(1024)
if (pDataRecv == pDataRecv):
if debug:
print ("Board OK")
except:
return False
return True
def get_socket(ip):
HOST = ip # The remote host
PORT = 58008 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
return s
if __name__ == "__main__":
ip = socket.gethostbyname(socket.gethostname())
HOST = '192.168.1.107' # The remote host
PORT = 58008 # The same port as used by the server
gameboards = [[[0 for i in range(10)] for j in range(10)] for k in range (4)]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
# try_connect(ip)
# s = get_socket(ip)
preamble = get_preamble(s)
print (preamble)
board = get_boards(s)
# if send_boards(s, gameboards):
# print ("Move submitted!")
s.close()