Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have client and server program. How can I make talker which combined server and client in one code, and how can client or server write more than one message(I think to use select()but I don't know how).

What I have tried:

I have programs for client and server.
Updated 14-Jun-21 8:29am
KarstenK 14-Jun-21 13:40pm    
when you really want that than use command line for configuration. Example: "udptalker.exe /server" and parse the args to start as server. And vice versa ...

1 solution

Combining a server and client probably isn't appropriate. They should run on different threads, which should also be separated from their work and GUI threads, respectively. All of them can be in the same .exe, though.

Although it may be more than what you're looking for, here's an example of a UDP thread. It only receives messages because the applications send messages without queueing them for output: UdpIoThread.cpp[^].
Share this answer
Member 15245946 14-Jun-21 13:38pm    
Thanks, and is it possible to have program, where you can send and receive messages, something like communication between to people?
This is my task: network programme enabling mutual
communication between 2 users in UDP. When on standard input, the user starts typing.
waits for the entire line and sends it to the other
when it receives a line from the network, it displays it on the standard
output (waits if the user is already typing)
Greg Utas 14-Jun-21 14:30pm    
Sure, it's possible, because there are many existing examples.

If it's just the two users, it's peer-to-peer client without a true server. Each client would have a thread for receiving UDP and a thread for the user interface. The UDP thread would queue an incoming message for the user interface thread, which would read it and display it. This ensures that the user interface doesn't get messed up by the UDP thread writing directly to it. You could probably find a way to safely update the user interface directly, but queueing a message is the way to do it when the UDP thread runs on a server, so coding it that way is good practice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900