Results 1 to 6 of 6

Thread: Using QTcpSocket to receive binary data

  1. #1
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Using QTcpSocket to receive binary data

    Hi,

    I wolud like to connect via ethernet with microcontroller. At the beginning i try with an example broadcast receiver and only think i change was
    Qt Code:
    1. QHostAddress address;
    2. address.setAddress(QString("192.168.20.1"));
    3. udpSocket->bind(address, QUdpSocket::ShareAddress);
    To copy to clipboard, switch view to plain text mode 
    Next i try use telnet (hyperterminal) to connect and send some data. I sets the same address like in qt, connect using: TCP/IP (Winsock) and port number 23. But the hyperterminal shows message window with Unable to connect.
    And my first question is how to send some data (binary or string) from application like hyperterminal (maybe other) to gui in qt using ethernet (QNetwork). Maybe is some software allows me to put some data to chosen address.

    Secondly i try to write my code but now using tcp/ip socket, not udp.
    Qt Code:
    1. void MainWindow::receivData()
    2. {
    3. if (receivSocket->waitForConnected(3000))
    4. {
    5. qDebug() << "Connected";
    6. }
    7. QByteArray buffer = receivSocket->read();
    8. bool ok;
    9. int data = buffer.toInt(&ok,10);
    10. ui->label->setText(QString::number(data));
    11. }
    12.  
    13. MainWindow::MainWindow(QWidget *parent) :
    14. QMainWindow(parent),
    15. ui(new Ui::MainWindow)
    16. {
    17. ui->setupUi(this);
    18. receivSocket = new QTcpSocket(this);
    19. connect(receivSocket,SIGNAL(readyRead()),this,SLOT(receivData()));
    20. QHostAddress address;
    21. address.setAddress(QString("192.168.20.1"));
    22. receivSocket->connectToHost(address,QIODevice::ReadOnly);
    23. }
    To copy to clipboard, switch view to plain text mode 
    and in .h
    Qt Code:
    1. #include <QtNetwork/QTcpSocket>
    2. ...
    3. private:
    4. QTcpSocket *receivSocket;
    5. private slots:
    6. void receivData();
    To copy to clipboard, switch view to plain text mode 
    I try to compile it but are some errors. error: no matching function for call to 'QTcpSocket::read()' Follow http://doc.qt.nokia.com/latest/network-programming.html QTcpSocket contains read(). My second question is what i should add/change in code if i want to receive some data.
    Thanks in advance for any help.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Using QTcpSocket to receive binary data

    Telnet uses TCP not UDP so trying to marry the two was never going to work.

    QTcpSocket does not contain a read() method that takes no arguments, which what your compiler is telling you about your code.

  3. #3
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QTcpSocket to receive binary data

    Quote Originally Posted by ChrisW67 View Post
    QTcpSocket does not contain a read() method that takes no arguments, which what your compiler is telling you about your code.
    Ok. I change it to correct form. But now when i'm debugging i gets errors:
    Qt Code:
    1. \mainwindow.cpp:7: error: undefined reference to '_imp___ZN15QAbstractSocket16waitForConnectedEi'
    2. \mainwindow.cpp:22: error: undefined reference to `_imp___ZN10QTcpSocketC1EP7QObject'
    3. \mainwindow.cpp:24: error: undefined reference to `_imp___ZN15QAbstractSocket13connectToHostERK7QStringt6QFlagsIN9QIODevice12OpenModeFlagEE'
    4. \mainwindow.cpp:24: error: undefined reference to `_imp___ZN15QAbstractSocket13connectToHostERK7QStringt6QFlagsIN9QIODevice12OpenModeFlagEE'
    5. \mainwindow.cpp:24: error: undefined reference to `_imp___ZN15QAbstractSocket13connectToHostERK7QStringt6QFlagsIN9QIODevice12OpenModeFlagEE'
    To copy to clipboard, switch view to plain text mode 
    Is this related with header? And what about rest of code. Is he correct? Maybe i missing somethink?

  4. #4
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Using QTcpSocket to receive binary data

    Post your complete code (mainwindow.cpp & mainwindow.h) to be sure and have some advice, it seems to me you are missing
    Qt Code:
    1. #include <QTcpSocket>
    To copy to clipboard, switch view to plain text mode 
    or maybe
    Qt Code:
    1. QT += network
    To copy to clipboard, switch view to plain text mode 
    in your .pro, but without any reproductible code to try can't be too sure.


    For your code, I don't understand why you are testing the connection's state in the slot connected to the readyRead() signal . If you want to be sure you are trully connected used waitForConnected() member or the connected() signal

  5. #5
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QTcpSocket to receive binary data

    Quote Originally Posted by nix View Post
    Qt Code:
    1. QT += network
    To copy to clipboard, switch view to plain text mode 
    Yes. It was it.
    I change my code.
    .cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QTcpSocket>
    4.  
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. text = new QLabel("",this);
    12. text->setText("");
    13. text->setGeometry(20,20,400,150);
    14. Test();
    15. }
    16.  
    17. void MainWindow::Test()
    18. {
    19. socket = new QTcpSocket(this);
    20.  
    21. connect(socket,SIGNAL(connected()),this,SLOT(connected()));
    22. connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    23. //connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
    24.  
    25. text->setText("Conecting...");
    26.  
    27. socket->connectToHost("169.254.19.60",23);
    28. if (!socket->waitForConnected(5000))
    29. {
    30. text->setText("Error: "+socket->errorString());
    31. }
    32. }
    33.  
    34. void MainWindow::connected()
    35. {
    36. text->setText("Connected");
    37. }
    38.  
    39. void MainWindow::readyRead()
    40. {
    41. QByteArray buf = socket->readAll();
    42. text->setText("Reading...\n"+buf);
    43. }
    44.  
    45. //void MainWindow::disconnected()
    46. //{
    47. // text->setText("Disconnected");
    48. //}
    49.  
    50. MainWindow::~MainWindow()
    51. {
    52. delete ui;
    53. }
    To copy to clipboard, switch view to plain text mode 
    .h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QTcpSocket>
    6. #include <QLabel>
    7.  
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. void Test();
    22.  
    23. private:
    24. Ui::MainWindow *ui;
    25.  
    26. QTcpSocket *socket;
    27. QLabel *text;
    28.  
    29. public slots:
    30. void connected();
    31. // void disconnected();
    32. void readyRead();
    33. };
    34.  
    35. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Now i'm getting error with timed out because i don't connect anything to HostAddress in connectToHost but i'm wondering if i connect to my PC microcontroller and send data from it to PC via ethernet will i get data i was send? I want to ask is my code correct to receive binary data? May i send to myself some data and read it using my program (if yes what to add, i try
    Qt Code:
    1. QByteArray writeBuf = "...Data...";
    2. socket->write(writeBuf);
    To copy to clipboard, switch view to plain text mode 
    but it don't working)? Thanks in advance.

  6. #6
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Using QTcpSocket to receive binary data

    If you want to test your code in local (using only your computer) you have to do your own TCP server based on (QTcpServer) take a look to Fortune server example.

    In your case the first thing you have to do is connecting. Without a connection you can't do anything in TCP socket. A socket is a bidirectionnal channel for communication data you wrote is send on the other side and data wrote by the other side is received by your app, that the basis.
    So it's perfectly normal that you do not received what you sent.

Similar Threads

  1. udp receive data problem
    By zxwmail in forum Newbie
    Replies: 9
    Last Post: 21st June 2011, 22:11
  2. receive data from client via QTcpServer
    By Fallen_ in forum Qt Programming
    Replies: 4
    Last Post: 8th September 2010, 16:08
  3. QTCPsocket data corrupt when receive
    By gabizzz in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2010, 17:29
  4. Replies: 4
    Last Post: 2nd September 2007, 19:48
  5. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17

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.