Results 1 to 15 of 15

Thread: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

  1. #1
    Join Date
    Feb 2006
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    I have the following code in my app. All this works, i tested by telneting to the machine running the QTcpServer code on port 4567 and I get the "pending connection" message.

    How do I do the following:

    1. accept the connection.
    2. Read the data coming through that connection.

    Any help is appreciated. Thanks in advanced.

    Qt Code:
    1. void mythread::run()
    2. {
    3. tcpServ = new QTcpServer;
    4.  
    5. connect(tcpServ = SIGNAL(newConnection()), this, SLOT(processConnection)));
    6.  
    7. tcpServ->listen(QHostAddress::Any, 4567);
    8.  
    9. exec();
    10. }
    11.  
    12. void mythread::processConnection()
    13. {
    14. if(tcpServ->hasPendingConnections())
    15. qDebug("Pending connection received");
    16. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by freak; 11th May 2006 at 17:48.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    You can retrieve a QTcpSocket object using QTcpServer::nextPendingConnection().

  3. #3
    Join Date
    Feb 2006
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    Will I have to create a QTcpSocket everytime there's a new connection/message that the QTcpServer receives?

    How would you read the data/msg coming in from the client machine in through the Socket?

    Thanks in advanced
    Last edited by freak; 11th May 2006 at 17:57.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default

    Quote Originally Posted by freak
    Will I have to create a QTcpSocket everytime there's a new connection/message that the QTcpServer receives?
    Not exactly, QTcpServer will create it for you for every connection it receives, but you can send/receive many messages using single QTcpSocket.

    Quote Originally Posted by freak
    How would you read the data/msg coming in from the client machine in through the Socket?
    You can use QIODevice::read() and QIODevice::write() or QTextStream or QDataStream.

  5. The following user says thank you to jacek for this useful post:

    freak (11th May 2006)

  6. #5
    Join Date
    Feb 2006
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    Thanks jacek, I'll look those options up in the docs and post how it went.

    Best regards.

  7. #6
    Join Date
    Feb 2006
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    Is the QTcpSocket created by the QTcpServer::nextPendingConnection() called " VT " by any chance? I output the QTcpServer::socketDescriptor() and that's what it shows.

    Best regards.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    Quote Originally Posted by freak
    I output the QTcpServer::socketDescriptor() and that's what it shows.
    This method returns an integer.

  9. #8
    Join Date
    Feb 2006
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    Ok I have the following:

    Qt Code:
    1. void mythread::run()
    2. {
    3. tcpServ = new QTcpServer;
    4. tcpSock = new QTcpSocket;
    5.  
    6. connect(tcpServ, SIGNAL(newConnection()), this, SLOT(processConnection()));
    7. connect(tcpSock, SIGNAL(readyRead()), this, SLOT(readMessage()));
    8.  
    9. tcpServ->listen(QHostAddress::Any, 4567);
    10.  
    11. exec();
    12. }
    13.  
    14. void mythread::processConnection()
    15. {
    16. tcpSock = tcpServ->nextPendingConnection();
    17. }
    18.  
    19. void mythread::readMessage()
    20. {
    21. QDataStream in(tcpSock);
    22. in.setVersion(QDataStream::Qt_4_1);
    23.  
    24. in >> message;
    25.  
    26. //where message is QString
    27. qDebug(message);
    28. }
    To copy to clipboard, switch view to plain text mode 

    I can't seem to read the messages sent from my client app. What's the best way of doing this? Thanks in advanced.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    Quote Originally Posted by freak
    tcpSock = new QTcpSocket;
    connect(tcpSock, SIGNAL(readyRead()), this, SLOT(readMessage()));
    ...
    tcpSock = tcpServ->nextPendingConnection();
    ...
    I can't seem to read the messages sent from my client app.
    First you create a socket and connect to its signal, then you get another socket from QTcpServer. It won't work, because that new socket isn't connected to any of your slots.

    What's the best way of doing this?
    See the examples that come with Qt.

  11. #10
    Join Date
    Feb 2006
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    Ok, I got the following code and now I can see the output from my client. I only see the first message though. All the rest of the messages sent from my client to the server are lost. I have to restart the client and then i get only the first mesage again.

    I checked the Qt Docs and the only example I see that receives multiple messages to the server automatically is the "Broadcast Receiver"

    http://doc.trolltech.com/4.1/network...eiver-cpp.html

    This is done using QUdpSocket. I tried a similar thing with QTcpSocket and apparently they don't share the same member functions.

    I'm trying to do the following:

    while receiving data from client
    display each individual message.

    Best regards.


    Qt Code:
    1. void mythread::run()
    2. {
    3. tcpServ = new QTcpServer;
    4.  
    5. connect(tcpServ, SIGNAL(newConnection()), this, SLOT(processConnection()));
    6.  
    7. if(!tcpServ->listen(QHostAddress::Any, 4567))
    8. {
    9. qDebug(tr("Unable to start the server: %1.").arg(tcpServ->errorString()));
    10. }
    11. exec();
    12. }
    13.  
    14. void mythread::processConnection()
    15. {
    16. QTcpSocket *clientConnection = tcpServ->nextPendingConnection();
    17.  
    18. char buf[1024];
    19. clientConnection->readLine(buf, sizeof(buf));
    20. qDebug(buf);
    21. }
    To copy to clipboard, switch view to plain text mode 

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    Quote Originally Posted by freak
    This is done using QUdpSocket. I tried a similar thing with QTcpSocket and apparently they don't share the same member functions.
    This approach won't work with TCP, because UDP is a connectionless protocol.

    In UDP you send messages (datagrams) without any connection, so from UDP's point of view each message has no relation with others --- they can even be delivered in different order, than they were sent. Your application listens for datagrams, when it gets one, it processes it and waits for another message.

    In TCP you create a connection between client and server (through this connection you can send a stream of bytes and TCP guarantees that each byte will be delivered in the same order as it was sent). Therefore your program must keep track of all connections.

    QTcpServer notifies you when new client connects and gives you a QTcpSocket which you can use to communicate with that client. You have to use this socket until the client disconnects, so you need some object that will handle the connection with a single client.

    You can have multiple clients connected to your server at the same time and each will have its own QTcpSocket.

    Here are some examples that use TCP:
    http://doc.trolltech.com/4.1/network...uneclient.html
    http://doc.trolltech.com/4.1/network-fortuneserver.html
    http://doc.trolltech.com/4.1/network...uneserver.html

  13. #12
    Join Date
    Feb 2006
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    Thanks for the information Jacek, I have been looking at fortune examples as reference but Im having problems understanding Qt's documentation. I can't see how you use the QTcpSocket created by the QTcpServer. Is my code above anywhere close to what it's supposed to be or am I just way lost? I appreciate the help.

    Worst case i'll probably memorize the Fortune Server/Client sources and during the process something might click.

    Best regards.

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    Here's an example:
    Qt Code:
    1. class ClientHandler : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. ClientHandler( QTcpSocket *socket ) : _socket( socket )
    6. {
    7. connect( _socket, SIGNAL( disconnected() ), this, SLOT( disconnected() ) );
    8. connect( _socket, SIGNAL( readyRead() ), this, SLOT( readyRead() ) );
    9. // ...
    10. }
    11.  
    12. private slots:
    13. // this method will be invoked when client disconnects
    14. void disconnected()
    15. {
    16. // ...
    17. deleteLater(); // delete ClientHandler instance
    18. }
    19.  
    20. // this will be invoked when client sends some data (remember that you receive a stream of bytes )
    21. void readyRead()
    22. {
    23. _buffer.append( _socket->readAll() );
    24. // while there is at least one complete message in the buffer:
    25. // parse & process the message
    26. // remove that message from the buffer
    27. }
    28.  
    29. private:
    30. QTcpSocket *_socket;
    31. QByteArray _buffer;
    32. };
    33.  
    34. // ...
    35.  
    36. void mythread::processConnection()
    37. {
    38. QTcpSocket *clientConnection = tcpServ->nextPendingConnection();
    39. if( clientConnection != 0 ) {
    40. new ClientHandler( clientConnection );
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 
    You can also derive ClientHandler from QThread and use the blocking interface (just like in threaded fortune server example).

  15. The following 2 users say thank you to jacek for this useful post:

    freak (15th May 2006), tpf80 (27th November 2006)

  16. #14
    Join Date
    Feb 2006
    Posts
    25
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    Thanks a lot for the sample code and the easy to read comments and explanation Jacek. This really helped a lot in both getting my app working and understanding more how Qt works. I really appreciate the help. I'm sure this thread will be helpful to more people.

    Best regards.

  17. #15
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: How does a QTcpServer process the data coming in? (Qt 4, SuSe10, gcc v4.0.2)

    Thanks for this example, after reading it the light bulb came on and now I finally that 2 way communication between my client and server.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.