I have this code. Its not the entire code because its a Blender addon with many lines that are unrelated to sockets so I give here only the part that deals with the sockets which I kept isolated from the rest of the program.
def create_thread():
global threadSocket,listening
threadSocket = threading.Thread(name='threadSocket', target= socket_listen)
listening = True
create_socket_connection()
threadSocket.start()
def socket_listen():
global receivedSocket,listening, receivedData,socketServer, socketMessages, pherror
socketServer.listen(5)
while listening:
(receivedSocket , adreess) = socketServer.accept()
receivedData = (receivedSocket.recv(1024)).decode("utf-8")[:-2]
socketMessages.append(receivedData)
receivedSocket.sendall('Hello from Blender!\n')
receivedSocket.close()
def create_socket_connection():
global socketServer
socketServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socketServer.bind(('127.0.0.1',4000))
If you want to see the entire code, it can be found here
I am using telnet to test the socket with telnet 127.0.0.1 4000, telnet can send the data but never receives any. As you can see my socket is blocking inside its own thread so it does not block the entire programs execution. I assume that sendall tries to send everything and for some reason it cannot connect back to telnet for sending . So the only problem here is sending data, if I remove the sendall line the code works fine and even with it , it has no problem receiving data. Am I doing something wrong here ?