I am trying to write a Tic Tac Toe game with sockets in python. There is a server file and a client file. When I run it (currently all on my computer), I run the server file, and then I run the client file twice (for X and for O). At some point I get a socket error (specified later in a comment, in the code from the server file).
This is the main method in the server file:
~~python
mat = initialize() # initializes the board (a matrix)
server_socket = socket.socket()
server_socket.bind(("0.0.0.0", 5555))
server_socket.listen(2)
(ix_socket, client_address) = server_socket.accept()
(o_socket, client_address) = server_socket.accept()
ix_socket.send("X".encode('utf-8')) # sends "X" to the first client
o_socket.send("O".encode('utf-8')) # sends "O" to the second client
mat_str = mat_to_mat_str(mat) # this function turns the board from matrix into a string so I can send it
ix_socket.send(mat_str.encode('utf-8'))
o_socket.send(mat_str.encode('utf-8'))
row=server_socket.recv(1)
here I get this error: "socket.error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied"
and the program stops...
col = server_socket.recv(1)
mat[row][col]="X" # should insert row and col received from client X
mat_str=mat_to_mat_str(mat) # should transform the modified board from matrix into string so I can send it
ix_socket.send(mat_str.encode('utf-8')) # should send mat_str to X client
o_socket.send(mat_str.encode('utf-8')) # should send mat_str to O client
This is the main method in the client file:
~~python
client_socket = socket.socket()
client_socket.connect(("127.0.0.1", 5555))
SIGN = client_socket.recv(1) # receives b"X" or b"O"
SIGN = SIGN.decode('utf-8')
mat_str = client_socket.recv(1024) # receives initial board in byte form
mat_str = mat_str.decode('utf-8') # turns bytes into string
mat = mat_str_to_mat(mat_str) # turns string into matrix (with function)
print("Initial board:")
print_mat(mat) # prints initial board (with no X's nor O's on it)
print("\r")
if SIGN == "X": # if you happend to be the first one to connect you are "X", and you should select a slot - for example 1a - where you want to place an X.
print("Insert row (1, 2, 3...):")
row = input()
print("Insert col (a, b, c...):")
col = input()
col = ord(col) - 96
col=str(col)
client_socket.send(row.encode('utf-8')) # sending row index
# oops, it is not being received by the server.
# from now on the program doesn't work if you are client X.
client_socket.send(col.encode('utf-8')) # sending col index
mat_str=client_socket.recv(1024) # receiving modified board in byte form
mat_str=mat_str.decode('utf-8') # turning it into string form
mat_str=mat_str_to_mat(mat_str) # turning it into matrix form
print("your board now:")
print_mat(mat) # printing modified board
elif SIGN == "O": # If you happened to be the second one to connect, you are O.
print("Waiting for X to play.")
mat_str = client_socket.recv(1024)
# receiving new board in byte form. It doesn't receive anything until client X plays. And he can't play, because the server doesn't receive the row index.
#so the following while loop runs forever:
while len(mat_str)==0: # waiting until it receives something
mat_str = client_socket.recv(1024)
mat_str = mat_str.decode('utf-8') # turning modified board from bytes into string
mat = mat_str_to_mat(mat_str) # turning string into matrix with function
print("your board now:")
print_mat(mat) # printing modified board