Results 1 to 5 of 5

Thread: QTcpSocket reading problem.

  1. #1
    Join Date
    Aug 2012
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default QTcpSocket reading problem.

    I have an application using QTcpSocket.

    for reading data i am using readline() as follows.

    socket.write(buf,buf.length);
    while(!socket.canReadLine());
    socket.read(rbuf,socket.bytesAvailable);

    But there is no data in my buf after reading and my gui getting freeze at bcoz of while loop.

    can any one tell me how to wait until data available in the socket?

    Thanks...

  2. #2
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTcpSocket reading problem.

    Use readyRead Signal. Write a class, which has several members (QByteArray buffer, QTcpSocket socket, ...) and a slot readyReadSlot(). Connect
    the readyRead Signal of the socket to your slot.

    Call socket.readAll() in your readyReadSlot() and append the returned QByteArray to your buffer member. Then check, if all data is in your buffer. If yes then process your data, if not return and wait for the next readyRead Signal of your socket.
    Pursue your targets and don't give up....

  3. #3
    Join Date
    Aug 2012
    Posts
    19
    Thanks
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket reading problem.

    on button click do this
    Qt Code:
    1. sock.connectToHost(ipaddr, port);
    2. QObject::connect(&sock, SIGNAL(readyRead()), this, SLOT(read()));
    To copy to clipboard, switch view to plain text mode 
    In read()
    Qt Code:
    1. while(sock.canReadLine())
    2. {
    3. QString line;
    4. line.setLength(4096);
    5. line = sock.readLine(); // read the line of text
    6. line=line.trimmed();
    7.  
    8. qDebug()<<"read"<<line;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Hope this helps
    On exit click
    close socket
    disconnect object

  4. #4
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: QTcpSocket reading problem.

    Don't forget to read QAbstractSocket documentation and learn about blocking and nonblocking socket.
    ~ We are nothing in this universe ~

  5. #5
    Join Date
    Aug 2012
    Posts
    33
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QTcpSocket reading problem.

    Thanks to all.........

Similar Threads

  1. QTcpSocket reading stucks
    By mentalmushroom in forum Qt Programming
    Replies: 10
    Last Post: 18th February 2011, 14:25
  2. Replies: 11
    Last Post: 1st December 2010, 11:28
  3. Problem with reading in data from a QTCPSocket
    By Denarius in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2009, 08:54
  4. QTcpSocket writing reading problem
    By Misenko in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2008, 07:27
  5. Replies: 6
    Last Post: 8th January 2007, 10:24

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.