Results 1 to 4 of 4

Thread: ZeroMQ with Qt client socket

Threaded View

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

    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.

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
  •  
Qt is a trademark of The Qt Company.