1

I'm trying to write a server-client(s) program in python using the socket and the threading module. I would like to send and receive to and from the client a confirmation data packet to check if it is still connected to the server and, simultaneously, I would like to be able to ask for input from the server user and then send it whenever the server user presses enter. I think a possible way to achieve that is just using two simultaneous threads, one that sends a validation data packet and the other that sands server user input. The syntax would look like the following:

import threading
import socket

def confirmation():
    # do confirmation

def send_server_input():
    # send the user server input

confirmation_thread = threading.Thread(target=confirmation)
confirmation_thread.start()

send_server_input_thread = threading.Thread(target=send_server_input)
send_server_input_thread.start

But I'm afraid that this way won't work, because the data packets may "get confused" or "collapse" one into the other. And if that doesn't happen, how can I distinguish one from the other from the client side this to data packets?

Thank you very much for your time and excuse my English, I'm still practising it!

Luke__
  • 227
  • 1
  • 9
  • If you delimit each message by defining a protocol for each transmitted message e.g. two bytes length, then the bytes of the message, then the receiver can synchronise with the received messages. If each end has more than one consumer of received messages then you should have a thread at each end dedicated to receiving messages and dispatching them to their correct recipient. Maybe your protocol should include a third byte ‘message type’ to help this. Or you could have a different socket for each type of message. Lots of choices. Probably examples of python doing this out there if you goggle – DisappointedByUnaccountableMod Nov 06 '20 at 22:24
  • Thank you very much for your time! Any idea or advice on some further reading? – Luke__ Nov 07 '20 at 06:57

0 Answers0