Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Nephatrine

Pages: [1]
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.

2
Network / Re: How to create a UDP "chat"
« on: December 28, 2012, 12:15:04 am »
What is your use case for wanting a chat program based off UDP? Just to experiment? Chat programs are usually more well-suited to TCP since reliability is pretty paramount to chat. I just finished a TCP chat lobby program using SFML2/SFGUI and it really wasn't too hard and wouldn't be difficult to port over to UDP. You are going to need to be either polling non-blocking sockets or doing your listening on threads, however, as otherwise you'll be stuck listening for new packets and never be able to do anything else.

As for the issue, you are having, can you clarify what you need done? Are you wanting the server to act as a client itself and chat from it or are you just wanting it to be able to forward the messages it receives to other client(s)? Is it going to just be the one server and one client... or were you going to allow for multiple clients at some point?

Pages: [1]
anything