PDA

View Full Version : client-server how?



nongentesimus
4th July 2006, 08:28
Hello Everybody

I would like to make a client-server arcitecture with Qt 4.0, using QTcpServer and QTcpSocket. I want one server and several, say 10 clients. I also want to communicate in both directions, sending messages from server to clients (which is easy) and from clients to servers (that's the hard part).
I tried to do it in the following way. I have a QTcpServer listening on one machine's port. I have several clients on other machines or port. First they all try to connect the server through the given hostname and port using connectToHost(). In the server whenever a new connection arrives I put the result of nextPendingConnection() to a vector containing sockets. Now this way it is easy to send anything to the clients from the server, I simply choose a socket from that vector and than send something on that socket (using QDataStream on the choosen socket, than write).
However, I can't see how can I send anything from the clients to the server. I use the client's socket to send some data. When data arrives in the server a readyRead() signal is emitted. But how can I know which socket emitted that signal? How can I choose the right socket from my vector? I cannot even send an ID with the client, because for reading that ID the server should first know which client sent that ID. So is there any solution, or my whole arcithecture is wrong this way? If it is wrong, could you sketch some other way.

ps. I saw that there was many thread about similar questions, but I could not find a satisfying answer anywhere, if You know about one I'm glad if you point to it.

Thanks for Your answers
Peter

nongentesimus
4th July 2006, 08:44
Ok, sorry I can see that the very same question was asked and answered three threads below. So QObject::sender() does exactly what I want. Sorry for asking again :o

siraansa
21st August 2006, 13:57
Hallo Peter,

Could you continue the thread, I am also working on similar project.

sudeepdua
28th November 2006, 09:05
thank you very much

sudeepdua
28th November 2006, 09:10
I'm facing a problem ... i have made the same application but i keep getting the string from the server 2 times when reading from the client even if i send it only once...... i have got some clue saying that i have to clear socket ..... but how >??

jpn
28th November 2006, 09:15
Maybe the Simple Chat (http://wiki.qtcentre.org/index.php?title=Simple_Chat) example in our wiki could be of some use.. ;)
It uses a QHash to store the clients and QObject::sender() to identify them. Pretty much something what nongentesimus already suggested as a solution.

jpn
28th November 2006, 09:25
I'm facing a problem ... i have made the same application but i keep getting the string from the server 2 times when reading from the client even if i send it only once...... i have got some clue saying that i have to clear socket ..... but how >??
Sounds like a problem of the buffering technique. Check out the example mentioned in my previous post. It uses QBuffer to handle all transmission data.