I am writing a chat program for my networking class and I have all the networking setup perfectly.
My problem is if a client is currently writing a message and he receives a message from a different client then his current input gets displayed with the received message.
For example if a client is writing a message "Hi there how are you?" and receives a message "Good day to you!" while in the middle of writing their message it gets displayed as:
Hi there hoGood day to you!
->w are you?
Where -> is the area for the user to type in the message. What I would like to happen is to just display the message received and have the area -> retain all the previous text that was written before the message was received.
Please make note that what the client is typing in is still in fact "there" when he receives a message. If he completes his message his full message will be sent.
Also note that my client uses pthreads. One thread to read messages from the server and display them to the users screen and one thread to read from stdin and send the messages to the server. I do believe that my problem is arising because I am using pthreads and the threads share the same stdin, stdout, stderr. Maybe this is a misconception and wrong?
I hope I have been clear on my problem. If not, sorry. Please let me know what I can clarify for you.
I started doing some research and came upon these links:
I was thinking about trying to go up lines and move the cursors around and stuff, but don't know if that is the most effective way to do so. Firstly because I don't know how to capture the information that is in the terminal waiting to "entered"/sent to stdin. Maybe I just haven't found out how to do that.
Also I was wondering if there was a way to work/manipulate file descriptors to solve the problem? Maybe that wouldn't even solve it?
Thanks for reading and your time. I appreciate all your help.