Advertisement

Why do I get unlimited [] after a message?

Started by June 13, 2004 11:58 PM
9 comments, last by kdogg 20 years, 8 months ago
The simplest way to send and receive, in my opinion, is to have a thread to receive messages and a thread to send them. For a chat program, there would be an outgoing queue and an incoming queue. The send thread would keeping querying the outgoing queue for messages. If it finds one, it would sent it off. The receive thread would read from the socket. When it gets a message, it would put it in the incoming queue and read from the socket again. The main thread would get input from the keyboard as well as query the incoming queue for messages coming in. When the user presses enter, the main thread would put the message in the outgoing queue.

Another way would be to use non blocking sockets. The program would try to receive a message. If recv returns 0, it would then get any user input and repeat. When a user presses enter, it would call send itself and then go back to square one.

Finally, you could also use select(). I don't usually mess around with select so i'll leave it up to someone else to hammer out the details for you.

This topic is closed to new replies.

Advertisement