Creating a basic UDP chat Program in C++

Go To StackoverFlow.com

2

I currently have a basic chat program in c++ that uses WinSock2.h with UDP. Currently the user is able to send a message to the server and the server just sends the same message back. I was wondering where do I go from here (i'm not asking for code). I was wondering how I should go forward in having the messages get sent to another client that is also connected to the server.

If I need to explain what I have done already please let me know.

All suggestions are greatly appreciated.

Thanks

2012-04-05 19:46
by user1219627
Why are you using UDP? You know that you could lose part of the chat if the packet is lost since there is no delivery guarantee unlike with TC - devshorts 2012-04-05 19:49
@devshorts i'm just working on making a basic chat..if a packet is lost it really isnt a big deal. Im just going for simplicity right no - user1219627 2012-04-05 21:11


3

You would have a list of currently connected users, when a user sends a message, it would then post it to all connected users.

Your server would keep track of who is connected, and remove those who get disconnected. When someone connect or disconnects, it would send a notification to all currently connected users, telling them of this notification.

All this is not specific to UDP, infact, TCP would probably be better for this type of messaging as you do not have to worry about messages being dropped. UDP should only be used where performance is of upmost importance, like real-time gaming, voice chat.

2012-04-05 19:48
by Matthew
so i was thinking about the client making a call to the server which would return an id for the client. How would i go forward in making this call? I was thinking about just sending an inital message the same way as if someone was to send text but I assume there has to be a better wa - user1219627 2012-04-05 21:24


2

When you're saying "connected" (in the context of clients) - what exactly do you mean?? Because you say you're using UDP in your program.

In the UDP protocol there's no "connected" state, unless you implement it.
In the TCP protocol however, there is (implemented within the protocol itself).

Furthermore, the basic idea of "broadcasting" a message is simple - keep a list of connected clients.
Add a client when it connects. Remove it from the list when it disconnects.
Then when you want to send a message to everyone you just iterate through this list.

Again, you'll have to receive those dis/connect events before you could keep track of "connected" clients.

If you go with TCP instead of UDP then you're set.

Good luck.

2012-04-05 19:50
by Poni
sticking with the UDP how can I make these calls to the server to add it to the list. Right now I am just aware of sending and recieving message - user1219627 2012-04-05 21:27
Look, if you insist on UDP then you'll have to make sure the server keeps a certain "registry" of "connected" clients. One way to do that is to have the client app send the server a message with code "I'm here" every 60 seconds, so the server knows the client is online. When the server needs to broadcast a message it iterates the clients list anyway, but before actually sending the message it will compare times and make sure that each client has sent the "I'm here" message within the last 60 seconds. Sort of time-out - Poni 2012-04-06 15:25


0

Basically, like Matthew said, you need to store all the current connections to the server. When a socket connects you can store a reference to that socket. Now whenever a client sends a message you can rebroadcast that to all the sockets. Now you have to handle when sockets disconnect as well since you don't want to store a bunch of closed sockets.

2012-04-05 19:51
by devshorts
Ads