I have a TCP client and a TCP server. Both of them are supposed to send data in order, I mean the client send then recv, the same for the server first recv and then send. But I don't know why my TCP client skips ALL THE RECV lines and send all the data instead of waiting for the server response. As far as I read, the sockets are in blocking mode by default Any suggestions?
UPDATE: I've added a minimal reproducible example:
Client side:
freq = 8
key = 1
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((self.HOST, self.PORT))
progress_callback.emit(1)
sock.sendall(bytes(TcpMsgs.INIT_MSG.value, "UTF-8"))
str(sock.recv(1024)).strip().lower()
progress_callback.emit(2)
sock.sendall(bytes(TcpMsgs.FREQ_MSG.value+str(freq), "UTF-8"))
str(sock.recv(1024)).strip().lower()
progress_callback.emit(3)
sock.sendall(bytes(TcpMsgs.KEY_LEN_MSG.value+str(key), "UTF-8"))
str(sock.recv(1024)).strip().lower()
progress_callback.emit(4)
sock.sendall(bytes(TcpMsgs.BEACON_MSG.value, "UTF-8"))
str(sock.recv(1024)).strip().lower()
progress_callback.emit(5)
#Wait till Bob reply with ok beacon
str(sock.recv(1024)).strip().lower()
#analyze msg and return error or ok
sock.close()
Server side:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
print("OPENING SOKCET")
s.bind(("",self.__port))
s.listen()
conn, addr = s.accept()
with conn:
progress_callback.emit(1)
print("Connected by:", addr)
conn.recv(1024)
progress_callback.emit(2)
conn.sendall(bytes(" ", "UTF-8"))
freq = float(str(conn.recv(1024)).split(":")[1])
progress_callback.emit(3)
self.__dataDictionary.update({BobVars.FREQ_DICT_KEY.value:freq})
conn.sendall(bytes("ok","UTF-8"))
key_len =int(str(conn.recv(1024)).split(":")[1])
progress_callback.emit(4)
self.__dataDictionary.update({BobVars.KEY_LEN_DICT_KEY.value:key_len})
conn.recv(1024)
progress_callback.emit(5)
conn.sendall(bytes("ok","UTF-8"))
#wait beacon ok
while(self.__waitFlag):
pass
progress_callback.emit(6)
#Beacon ok
conn.sendall(bytes("ok","UTF-8"))
self.__waitFlag = not self.__waitFlag
rand_array = pickle.dumps([random.randint(0,1) for _ in range(int(key_len))])
conn.sendall(rand_array)