0

I'm having an issue where outputs from two different clients in a Python client-server TCP application are interleaving with each other. For example:

Client connected to 0.0.0.0:3000
hello world
[1.1.1.1:55845] hello world from client 2
how are[1.1.1.1:55845] hello world again
you doing

In this example, client 1 says "hello world" and client 2 says "hello world from client 2". When client 1 starts to type, client 2 sends a message "hello world again", which interleaves with the message that client 1 is about to type. Then, client 1 is forced to type the rest of the message on the next line.

Is there a way to make sure that when client 1 is typing a message, client 2's message either goes on a line above or generally does not interfere with the line the text is being typed on?

Because this is a Python client-server program, I saw the curses module, but I'm not sure whether this is the right approach or whether this could solve my problem. Is there a way to have a lock on the Terminal screen maybe?

Shrey
  • 522
  • 8
  • 16
  • If the messages are sent/received as a whole (e.i. you only see them after the other person hits enter) then its just a matter of using a queue to display them one after another. If you want real-time, then things are gonna get a lot more tricky. – AlanSTACK Dec 31 '17 at 23:22
  • Yeah, it's real-time right now. – Shrey Dec 31 '17 at 23:27
  • then you are in for one hell of a ride. Check out this video for an introduction on how python interacts with the terminal https://www.youtube.com/watch?v=WAitSilLDUA – AlanSTACK Dec 31 '17 at 23:31
  • `curses` certainly is one feasible approach. See my answer to [Simultaneous input and output for network based messaging program](https://stackoverflow.com/a/34992925/2413201). – Armali Nov 18 '19 at 11:48

0 Answers0