1
Network / Re: How to create a UDP "chat"
« on: December 28, 2012, 10:12:51 pm »
UDP is not "reliable" which means packets may be dropped and aren't guaranteed to reach the destination in order or even at all. That makes it a poor choice for chat unless you want to do a lot of additional coding to tack on that functionality. Really the only way that it's simpler is that it is connection-less and your server would just need the one socket to talk to all the clients rather than having to use a SocketSelector. If you just want the one client, however, you can do without a SocketSelector and just have a Listener to accept connections and another Socket for the client once it connects. Look at the chat example eXpl0it3r linked to see how to do almost exactly what you're currently doing with TCP.
To have the client and server both receive and send messages, you'll either need to use non-blocking sockets (though you won't want to use std::cin since it blocks) or use multiple threads. I prefer using threads.
SERVER
- Wait for connection on listener.
CLIENT
- Connect to server.
BOTH
- Once you have a connection, spawn a thread listening on client socket for messages to display.
- In main loop, handle user input and send messages out.
If the client disconnects, the server will need to go back to waiting for connections. This is not an ideal setup but is very simple. Since you're doing everything from the console with std::cin and std::cout, however, you'll probably end up with interleaved output and input in the console due to the threading. For instance, if you're halfway through typing something to send and a message is received, you'll really confuse things if you just write it to the console.
In a better client-server architecture, you'd want the server to just listen and forward messages to other clients (aside from the original sender). This server wouldn't take any user input and would just be background process. You could then just spawn two clients and test by talking to eachother. You'd need to use a SocketSelector on the server to listen for both incoming connections on the Listener and to all the connected client Sockets at the same time. You'd also want to come up with some way to uniquely identify each connected user so everyone knows who is saying what.
I have a very basic multi-user chat server-client system I got working as part of a larger project. I could probably excise the chat-room functionality and post it somewhere for others to experiment with. My client interface uses SFGUI rather than the console, however, so I'm not sure if that would just confuse matters even more for someone new to SFML who just wants a network example.
To have the client and server both receive and send messages, you'll either need to use non-blocking sockets (though you won't want to use std::cin since it blocks) or use multiple threads. I prefer using threads.
SERVER
- Wait for connection on listener.
CLIENT
- Connect to server.
BOTH
- Once you have a connection, spawn a thread listening on client socket for messages to display.
- In main loop, handle user input and send messages out.
If the client disconnects, the server will need to go back to waiting for connections. This is not an ideal setup but is very simple. Since you're doing everything from the console with std::cin and std::cout, however, you'll probably end up with interleaved output and input in the console due to the threading. For instance, if you're halfway through typing something to send and a message is received, you'll really confuse things if you just write it to the console.
In a better client-server architecture, you'd want the server to just listen and forward messages to other clients (aside from the original sender). This server wouldn't take any user input and would just be background process. You could then just spawn two clients and test by talking to eachother. You'd need to use a SocketSelector on the server to listen for both incoming connections on the Listener and to all the connected client Sockets at the same time. You'd also want to come up with some way to uniquely identify each connected user so everyone knows who is saying what.
I have a very basic multi-user chat server-client system I got working as part of a larger project. I could probably excise the chat-room functionality and post it somewhere for others to experiment with. My client interface uses SFGUI rather than the console, however, so I'm not sure if that would just confuse matters even more for someone new to SFML who just wants a network example.