QTcpSocket questions
-
I've got a rather large assignment I'm trying to get started on. Basically, the easiest way to describe it is that I have to design and code an Instant Message solution. My professor wants both the client and server side programs and wants me to use the TCP protocol and a GUI.
I decided to use Qt because the easiness of its GUIs and it's network support. However, in designing the server side program, I'm running into an issue with QTcpSocket. I can't figure out the right way to do it. How do I designate a port to listen on? How do I send new data back to the client? Do I need one socket to receive and one one to send?
As of now, I have a class to handle the sending and receiving, sending and connection management. For opening the port to receiving, I used
@Socket->bind(8060);@
Now, I have no idea if that's the correct syntax to listen on that port. To send, do I have to have a new socket and connect to the client? Would I have to do something like
@socket2->connectToHost("destIP",destPort);@
and
@socket2->write(data);@
The Qt framework is really new to me and I've just recently decided to start experimenting with it. I have to kind of take the crash course on the networking portion to get this done and I really appreciate any help in making this work.
Sean
-
Have a look at QTCPServer documentation and the examples.
In the typical client/server arrangement, where clients chat with each other via the server, then the clients connect to the server and the connection is held open until the client disconnects. That is, each new connect results in a new QTcpSocket corresponding to that client. Your server has to look after routing messages coming in through one socket and send it to the correct receiver's socket.
An alternate arrangement is where the server only exists to allow clients to register their existence and locate other clients. Clients connect directly to each other to send message data. This is more complex.
-
I've been reading the documentation and can't seem to find the method that opens the socket so that it can be connected to. I don't know which will make the socket listen. Having a new socket for each client does make sense, but I still don't understand how it will listen. Everything else makes sense.
As far as using either a client/server type model or a peer-to-peer type model, this assignment has me doing both. But I wanted to tackle the easier one first so that I can get a better grasp on the harder one.
-
How about "QTcpServer::listen(..)":http://qt-project.org/doc/qt-4.8/qtcpserver.html#listen ?
Then when you receive the signal "newConnection()":http://qt-project.org/doc/qt-4.8/qtcpserver.html#newConnection from the QTcpServer you can get the socket for this connection by calling "nextPendingConnection":http://qt-project.org/doc/qt-4.8/qtcpserver.html#nextPendingConnection and send and receive via this socket.
Maybe the "FortuneServer":http://qt-project.org/doc/qt-4.8/network-fortuneserver.html and "Fortune Client":http://qt-project.org/doc/qt-4.8/network-fortuneclient.html examples are useful for you.
-
Thanks a lot. I'll read those. They seem like they'll really help.