Results 1 to 7 of 7

Thread: Can't read from socket

  1. #1
    Join Date
    Dec 2010
    Location
    Poland, Szczecin
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Can't read from socket

    Hi, I was trying to port simple socket application ffrom pure C to QT4. In C i have all my wanted functionality. The application just should send one byte to Host, and then get the answer from the host.

    I'm trying to do it like this :
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QtNetwork/QTcpSocket>
    3. #include <QByteArray>
    4.  
    5.  
    6.  
    7. using namespace std;
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11.  
    12. char received[2048];
    13.  
    14. QCoreApplication a(argc, argv);
    15. QTcpSocket sock;
    16.  
    17. char data[20]="lalalala";
    18.  
    19. sock.connectToHost("10.0.1.1",23 , QIODevice::ReadWrite);
    20. sock.write(data);
    21. sock.readLine(received, 5);
    22. qDebug() << received ;
    23.  
    24. sock.disconnectFromHost();
    25. return a.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    In wireshark i can see that my PC is sending the "lalalala" string to this IP, also in the wireshark i see that host 10.0.1.1 is answering me, but in i have no data in received buffer.

    So what i'm doing wrong ?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can't read from socket

    You should use QAbstractSocket::waitForBytesWritten and QAbstractSocket::waitForReadyRead for reading/writing data. Also you should wait until a socked is connected using QAbstractSocket::waitForConnected.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Can't read from socket

    Or, for non blocking implementation, use signals readyRead() - read data, use simply write and stateChanged() to know current socket state.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  4. #4
    Join Date
    Dec 2010
    Location
    Poland, Szczecin
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't read from socket

    Hello,
    OK, so i've done it like this, main class constructor has the connect:

    Qt Code:
    1. QObject::connect(&socket,SIGNAL(readyRead ()),this,SLOT(receiveData()));
    To copy to clipboard, switch view to plain text mode 

    After I am connected to host (wireshark confrm that)

    there is still no call of receiveData() slot... ?

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't read from socket

    Quote Originally Posted by marekdor View Post
    Hello,
    OK, so i've done it like this, main class constructor has the connect:

    Qt Code:
    1. QObject::connect(&socket,SIGNAL(readyRead ()),this,SLOT(receiveData()));
    To copy to clipboard, switch view to plain text mode 

    After I am connected to host (wireshark confrm that)

    there is still no call of receiveData() slot... ?
    The slot will only be called once the event loop is running. You seem to have chosen the asynchronous/non-blocking approach, which should have brought major changes to the structure of you program. Please show the complete code.

    I also suggest you study the Fortune client example in Qt's documentation.

  6. #6
    Join Date
    Dec 2010
    Location
    Poland, Szczecin
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't read from socket

    Ok, here it is :

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QPushButton>
    4. #include <QDebug>
    5. #include <QIODevice>
    6. #include <QtNetwork/QTcpSocket>
    7.  
    8.  
    9.  
    10. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
    11. ui(new Ui::MainWindow)
    12. {
    13. ui->setupUi(this);
    14.  
    15. QTcpSocket socket;
    16. ui->disconnectButton->setDisabled(true);
    17. QObject::connect(&socket,SIGNAL(readyRead ()),this,SLOT(receiveData()));
    18.  
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    25.  
    26. void MainWindow::connectHandler()
    27. {
    28. int n;
    29. char dataToSend[2]={0x1e};
    30. char received[2048]={0};
    31. socket.connectToHost(ui->ipAddress->displayText(),ui->portNumber->displayText().toInt(), QIODevice::ReadWrite);
    32.  
    33. if (socket.waitForConnected(1000))
    34. {
    35. qDebug() << "Connecteed! " ;
    36. socket.write(dataToSend);
    37.  
    38. socket.waitForBytesWritten(3000);
    39. qDebug() << socket.readAll();
    40.  
    41. qDebug() << received ;
    42.  
    43. ui->connectButton->setDisabled(true);
    44. ui->disconnectButton->setEnabled(true);
    45. }
    46. }
    47.  
    48. void MainWindow::disconnectHandler()
    49. {
    50. this->socket.disconnectFromHost();
    51. if (socket.state() == QAbstractSocket::UnconnectedState ||
    52. socket.waitForDisconnected(1000))
    53. {
    54. qDebug("Disconnected!");
    55. ui->disconnectButton->setDisabled(true);
    56. ui->connectButton->setEnabled(true);
    57. }
    58. }
    59.  
    60. void MainWindow::receiveData()
    61. {
    62.  
    63. qDebug() << "dew data!!!" ; //this never happen... :(
    64.  
    65. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't read from socket

    Firstly, I think you should not mix blocking-style and non-blocking-style operations on the socket. It works, but it is not very elegant. For example, instead of calling waitForConnected(), you could connect the socket's connected() signal to a slot in your main window to which you would move the code writing data to the socket. Also, you do not need to call waitForBytesWritten(): if you simply return to the event loop, the socket will automatically take care of sending the data at the right moment.

    In any case this should not prevent the socket from emitting its readyRead() signal. I have several questions for you:
    - On line 39 you read all the data from the socket. What happens if you comment this line out?
    - Are there any error messages in the application's output relating to QObject::connect()?
    - Have you tried connecting to the disconnected() and error() signals of the socket? Some error might be occurring before the socket has any chance of receiving data.

Similar Threads

  1. Read all_ tcp socket
    By Mayssa in forum General Programming
    Replies: 1
    Last Post: 21st February 2012, 17:12
  2. Replies: 2
    Last Post: 22nd May 2011, 21:31
  3. socket read/write bytes
    By nowire75 in forum Newbie
    Replies: 3
    Last Post: 4th July 2007, 23:12
  4. Replies: 2
    Last Post: 23rd June 2007, 09:13
  5. Replies: 3
    Last Post: 4th July 2006, 10:15

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.