PDA

View Full Version : Socket Problem.



ayanda83
17th November 2013, 18:51
hi everyone, I'm creating a client server app, a chat app to be specific. I need to know how i can implement the ability for one client to communicate with another client connected to the same server. The clients can connect fine to the server the problem is getting a message from one client to the other via the server. thanking you in advance.

toufic.dbouk
17th November 2013, 20:55
Since both clients are connected to the server , one approach would be send to the server and let the server send the message to the other client through the socket.
you might want to send the message with some info of the other client maybe IP address , name , ID depending on your implementation. And according to that info the server knows to whom the redirection of the message should be. That's a general idea that might be helpful.
Another approach would be Peer to peer (P2P) if you wanna give that a try.
Good Luck.

anda_skoa
18th November 2013, 11:18
You have three options

1) as toufic.dbouk said, having clients connect to each other, using the server connection only to exchange connection details. Obviously requires that at least one party is reachable for TCP connects, i.e. has appropriate firewall setup

2) have one connection to the server for connection management and one for each client/client conversation

3) multiplex multiple chat channels over a single connection using some form of protocol

Cheers,
_

KShots
18th November 2013, 19:25
For a simple chat app, I'd tend to lean towards one connection per client to the server, and all messaging goes through the server. This would mean that your messaging protocol would handle the routing - each message hits the server, then the server sends the message to the appropriate destination (all, or perhaps a single client, or an error message if that client is no longer connected). You'd simply listen for traffic on the server, have some means of determining message type (perhaps via google protocol buffers (https://code.google.com/p/protobuf/) or something similar through the QDataStream), and based on that type, do something with the message. Pretty simple stuff...

Now, if you want to handle more than simple chat-related data, you may want to consider direct client->client connections so you don't saturate your server's bandwidth with traffic that really has nothing to do with it. The above method gets around most firewall issues (the server is the only thing listening, and hence is the only thing that needs to have ports opened), while this method may run into firewall issues (each client needs to listen for connections from each other). Also, the above method allows for logging (if you desire it), while direct client->client connections can only be logged by the clients involved. I guess in the end, it really depends on what you want to do with it.