Results 1 to 4 of 4

Thread: ZeroMQ with Qt client socket

  1. #1
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default ZeroMQ with Qt client socket

    I've recently started on a project which requires me to write a script in order to send/receive strings on the pc, in which I've decided and written a piece of code to allow the pc to communicate using zeromq in a qt app that I'm currently building. However, I'm facing some issues with the zeromq bindings (nzmqt) for qt and the client (pc side) can't seem to connect to the server address/port(a separate device on the same network). After numerous attempts to debug it, I can't seem to find out what have I done wrong, and the scarce amount of documentation for the bindings aren't really helping much either. The pc app compiles without errors and is able to run, but the sockets aren't connecting at all. I was hoping that someone might have done something similar and could assist on this issue of mine.

    socket.cpp
    Qt Code:
    1. #include "socket.h"
    2. #include <QByteArray>
    3. #include <QList>
    4. #include <QSignalSpy>
    5.  
    6. nzmqt::Socket::Socket(QObject* parent) :
    7. QThread(parent)
    8. {
    9. m_address = "tcp://127.0.0.1:8887";
    10. ZMQContext* context = createDefaultContext(this);
    11. context->start();
    12.  
    13. m_socket = context->createSocket(ZMQSocket::TYP_PUSH);
    14. connect(m_socket, SIGNAL(connected()), SLOT(sendString()));
    15.  
    16. r_address = "tcp://127.0.0.1:8886";
    17. r_socket = context->createSocket(ZMQSocket::TYP_SUB);
    18. connect(r_socket, SIGNAL(recvString(const QList<QByteArray>&)), SLOT(recvString(const QList<QByteArray>&)));
    19.  
    20. timerout = new QTimer(this);
    21. timerout->setInterval(2500);
    22. timerout->setSingleShot(true);
    23.  
    24. connect(timerout, SIGNAL(timeout()), this, SLOT(timerout_timeout()));
    25.  
    26. client = new nzmqt::Socket(this);
    27. client->start();
    28.  
    29. }
    30.  
    31. nzmqt::Socket::~Socket()
    32. {
    33. client->stop();
    34. }
    35.  
    36. void nzmqt::Socket::run()
    37. {
    38. m_socket->connectTo(m_address); //send
    39. r_socket->connectTo(r_address); //feedback
    40.  
    41. timerout->start();
    42. this->exec();
    43. }
    44.  
    45. void nzmqt::Socket::stop()
    46. {
    47. timerout->stop();
    48. m_socket->close();
    49. this->quit();
    50. }
    51.  
    52. void nzmqt::Socket::send(QString message) //taking an emitted string from a button on the app
    53. {
    54. this->message = message;
    55. }
    56.  
    57. void nzmqt::Socket::sendString()
    58. {
    59. m_socket->sendMessage(signal.toStdString().c_str());
    60. emit socketStarted(signal);
    61. QTimer::singleShot(0, this, SLOT(stop()));
    62. }
    63.  
    64. void nzmqt::Socket::recvString(const QList<QByteArray>& message) //print feedback
    65. {
    66. qDebug() << message;
    67. }
    To copy to clipboard, switch view to plain text mode 

    socket.h
    Qt Code:
    1. #ifndef SOCKET_H
    2. #define SOCKET_H
    3.  
    4. #include <nzmqt/nzmqt.hpp>
    5.  
    6. #include <QByteArray>
    7. #include <QDateTime>
    8. #include <QList>
    9. #include <QTimer>
    10. #include <QThread>
    11.  
    12. namespace nzmqt
    13. {
    14.  
    15. class Socket : public QThread
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. Socket(QObject *parent=0);
    21. ~Socket();
    22. void run();
    23.  
    24. public slots:
    25. void stop();
    26. void sendString();
    27. void send(QString message);
    28.  
    29. signals:
    30. void socketStarted(QString signal);
    31.  
    32. protected slots:
    33. void recvString(const QList<QByteArray>& message);
    34.  
    35. private:
    36. QString message;
    37. QString m_address;
    38. QString r_address;
    39.  
    40. ZMQSocket* m_socket;
    41. ZMQSocket* r_socket;
    42. QTimer* timerout;
    43. Socket *client;
    44. };
    45. }
    46. #endif // SOCKET_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by ashboi; 10th October 2013 at 09:26.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: ZeroMQ with Qt client socket

    One problem is that you create a QTimer in the context of the caller thread (constructor) but try to access it in the context of the worker thread (run).
    You should also be aware that you are connecting it to a slot that belongs to an object in the caller thread, so it will be executed by the caller thread. Not sure if this is what you intend to happen.

    Since threads tend to make everything more complicated, have you tried doing it without threads first?

    Cheers,
    _

  3. #3
    Join Date
    May 2012
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: ZeroMQ with Qt client socket

    I haven't tried without threads yet, but I've gotten my client socket working. However, I was wondering how should I modify my codes in a such a way that the when the app runs, the socket waits until a signal(string) is emitted from a pushbutton(on another form), and proceed to send this string over to the server, and continue to wait until another signal has been emitted/received.

    I'm quite new in working with Qt, but I currently got the signal to emit the correct strings on another form (form B), and the socket is running in main.cpp, I can't seem to receive the emitted signals on my socket thread. I've tested by starting the socket thread in form B instead of main.cpp, I can receive the signals but it doesn't wait indefinitely initially until the button is clicked(since Qtimer is set at 1000ms), and after sending the strings, it doesn't wait again until another signal is received from the same pushbutton. I really hope someone could assist me on this.

    socket.cpp
    Qt Code:
    1. #include "socket.h"
    2. #include <QByteArray>
    3. #include <QList>
    4. #include <QSignalSpy>
    5.  
    6. using namespace nzmqt;
    7.  
    8. Socket::Socket(QObject* parent) :
    9. QThread(parent)
    10. {
    11. m_address = "tcp://127.0.0.1:8887";
    12. ZMQContext* context = createDefaultContext(this);
    13. context->start();
    14.  
    15. m_socket = context->createSocket(ZMQSocket::TYP_PUSH);
    16.  
    17. topic_ = "";
    18. r_address = "tcp://127.0.0.1:8888";
    19. r_socket = context->createSocket(ZMQSocket::TYP_SUB);
    20. connect(r_socket, SIGNAL(messageReceived(const QList<QByteArray>&)), SLOT(messageReceived(const QList<QByteArray>&)));
    21.  
    22. timerout = new QTimer(this);
    23. timerout->setInterval(2500);
    24. timerout->setSingleShot(true);
    25.  
    26. }
    27.  
    28. Socket::~Socket()
    29. {
    30. client->stop();
    31. }
    32.  
    33. void Socket::run()
    34. {
    35. m_socket->connectTo(m_address);
    36. QTimer::singleShot(1000, this, SLOT(sendMessage()));
    37.  
    38. r_socket->subscribeTo(topic_);
    39. r_socket->connectTo(r_address);
    40.  
    41. timerout->start();
    42. this->exec();
    43. }
    44.  
    45. void Socket::stop()
    46. {
    47. timerout->stop();
    48. m_socket->close();
    49. this->quit();
    50. }
    51.  
    52. void Socket::send(QString message) //received emitted signal
    53. {
    54. this->message = message;
    55. }
    56.  
    57. void Socket::sendMessage() //send string
    58. {
    59. QList< QByteArray > msg;
    60. msg+= message.toLocal8Bit(); //testing
    61. m_socket->sendMessage(msg);
    62. qDebug() << msg; //for testing
    63. emit stringSent(msg);
    64. QTimer::singleShot(0, this, SLOT(stop()));
    65. }
    66.  
    67. void Socket::messageReceived(const QList<QByteArray>& message) //display feedback
    68. {
    69. qDebug() << message;
    70. }
    To copy to clipboard, switch view to plain text mode 

    socket.h
    Qt Code:
    1. #ifndef SOCKET_H
    2. #define SOCKET_H
    3.  
    4. #include <nzmqt/nzmqt.hpp>
    5.  
    6. #include <QByteArray>
    7. #include <QDateTime>
    8. #include <QList>
    9. #include <QTimer>
    10. #include <QObject>
    11. #include <QThread>
    12.  
    13. namespace nzmqt
    14. {
    15.  
    16. class Socket : public QThread
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. Socket(QObject *parent=0);
    22. ~Socket();
    23. void run();
    24.  
    25. public slots:
    26. void stop();
    27. void send(QString message); //receive emitted signal
    28.  
    29. signals:
    30. void stringSent(const QList<QByteArray>& message);
    31.  
    32.  
    33. protected slots:
    34. void messageReceived(const QList<QByteArray>& message);
    35. void sendMessage();
    36.  
    37. private:
    38. QString message;
    39. QString m_address;
    40. QString r_address;
    41. QString r_address1;
    42. QString topic_;
    43.  
    44. ZMQSocket* m_socket;
    45. ZMQSocket* r_socket;
    46. ZMQSocket* r_socket1;
    47. QTimer* timerout;
    48. Socket *client;
    49. };
    50. }
    51. #endif // SOCKET_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by ashboi; 13th October 2013 at 09:11.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: ZeroMQ with Qt client socket

    I would strongly suggest to make it work in a single threaded context first.

    It avoids all kinds of mistakes, e.g, creating a timer in one thread and starting/stopping it from another (like you do with timerout) or making wrong assumption about thread context in slots (you might think that sendMessage() is called by the socket thread).

    Once you have it running in a single thread it is way easier to move it into a thread and just to deal with the necessary changes then, instead of trying everything in one go.
    Especially when being new to multithreading with Qt

    Cheers,
    _

Similar Threads

  1. network client socket without using signals and slots
    By erqsor in forum Qt Programming
    Replies: 7
    Last Post: 24th March 2011, 09:28
  2. simple socket client example
    By Tomasz in forum Newbie
    Replies: 4
    Last Post: 26th July 2010, 13:15
  3. Client Socket
    By electronicboy in forum General Programming
    Replies: 1
    Last Post: 12th October 2009, 12:20
  4. Replies: 1
    Last Post: 27th March 2009, 13:20
  5. Programming client-server with socket in multi-languages
    By philiptine in forum Qt Programming
    Replies: 3
    Last Post: 7th September 2007, 07:35

Tags for this Thread

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.