Results 1 to 7 of 7

Thread: problems using QTcpSocket

  1. #1
    Join Date
    Jan 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default problems using QTcpSocket

    Hello,

    I want to implement a TCP client connection that can recieve messages and reponses. For that I use a Qqueue for putting the response of my request and I use the readyRead signal to see if there are incoming response/requests.

    My code looks as follows


    Qt Code:
    1. connect(tcpSocket,SIGNAL(readyRead()),this, SLOT(readData()));
    2.  
    3. ......
    4.  
    5. response_ptr Client:: sendRequest(QByteArray const &data) {
    6.  
    7. qDebug() << "Request: " << data.constData();
    8. tcpSocket->write(data);
    9. while(response_queue.isEmpty()) {}
    10. return response_ptr(response_queue.dequeue());
    11.  
    12. }
    13.  
    14. void Client::readData() {
    15.  
    16. .....
    17. // Read response an put it on the queue
    18.  
    19. response_queue.queue(response)
    20. }
    21.  
    22. .....
    To copy to clipboard, switch view to plain text mode 

    if I run the code and call "sendRequest", I come into a loop. Using wireShark I see that the request isn't send to the server.It seems that QT doesn't create a thread for listening to incoming data. I think that everything is running on the same thread ? So do I need to create a thread for listening on incoming messages/ responses ?

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problems using QTcpSocket

    Quote Originally Posted by HERC View Post
    Hello,

    Qt Code:
    1. while(response_queue.isEmpty()) {}
    2. return response_ptr(response_queue.dequeue());
    To copy to clipboard, switch view to plain text mode 
    I think that this is a programming error. The "return" statment exits the function, you don't get a while loop as it have returned to the caller instruction.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Jan 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problems using QTcpSocket

    Hello,





    On line 8 I put some data on the socket. But it seems that it is not send to the server. On line 9 I come into an endless loop, since I don't recieve any responses. If I delete line 9, then te message is send to the server and I recieve a response.

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problems using QTcpSocket

    Hi,

    Try this:
    Qt Code:
    1.  
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problems using QTcpSocket

    Hi,

    Try this:
    Qt Code:
    1. tcpSocket->write(data);
    2. tcpSocket->flush;
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  6. #6
    Join Date
    Jan 2010
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: problems using QTcpSocket

    Using
    Qt Code:
    1. tcpSocket->flush();
    To copy to clipboard, switch view to plain text mode 
    it still not works.

  7. #7
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problems using QTcpSocket

    you have to divide sending and receiving. In sendRequest() just send your data and return. QTcpSocket works in asynchronous mode so it will inform you with a signal about incoming data. But doing
    Qt Code:
    1. while (sth) { }
    To copy to clipboard, switch view to plain text mode 
    just block event loop so I guess it won't work at all.
    Just return and happily wait for readyRead(). Make a signal in your class and emit it when response is ready (so after reading it from socket after readyRead()).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

Similar Threads

  1. Problems with maximum data sent over a QTcpSocket
    By jordip in forum Qt Programming
    Replies: 8
    Last Post: 6th July 2010, 15:34
  2. Need Help with QTcpSocket
    By jknotzke in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2009, 14:55
  3. QTcpSocket
    By pdoria in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2009, 18:52
  4. QT4.2.2 and QTcpSocket
    By Nyphel in forum Qt Programming
    Replies: 13
    Last Post: 11th March 2007, 12:30
  5. Problems with QThread and QTcpSocket
    By cookiem in forum Qt Programming
    Replies: 6
    Last Post: 2nd November 2006, 09:25

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.