ashboi
10th October 2013, 07:58
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
#include "socket.h"
#include <QByteArray>
#include <QList>
#include <QSignalSpy>
nzmqt::Socket::Socket(QObject* parent) :
QThread(parent)
{
m_address = "tcp://127.0.0.1:8887";
ZMQContext* context = createDefaultContext(this);
context->start();
m_socket = context->createSocket(ZMQSocket::TYP_PUSH);
connect(m_socket, SIGNAL(connected()), SLOT(sendString()));
r_address = "tcp://127.0.0.1:8886";
r_socket = context->createSocket(ZMQSocket::TYP_SUB);
connect(r_socket, SIGNAL(recvString(const QList<QByteArray>&)), SLOT(recvString(const QList<QByteArray>&)));
timerout = new QTimer(this);
timerout->setInterval(2500);
timerout->setSingleShot(true);
connect(timerout, SIGNAL(timeout()), this, SLOT(timerout_timeout()));
client = new nzmqt::Socket(this);
client->start();
}
nzmqt::Socket::~Socket()
{
client->stop();
}
void nzmqt::Socket::run()
{
m_socket->connectTo(m_address); //send
r_socket->connectTo(r_address); //feedback
timerout->start();
this->exec();
}
void nzmqt::Socket::stop()
{
timerout->stop();
m_socket->close();
this->quit();
}
void nzmqt::Socket::send(QString message) //taking an emitted string from a button on the app
{
this->message = message;
}
void nzmqt::Socket::sendString()
{
m_socket->sendMessage(signal.toStdString().c_str());
emit socketStarted(signal);
QTimer::singleShot(0, this, SLOT(stop()));
}
void nzmqt::Socket::recvString(const QList<QByteArray>& message) //print feedback
{
qDebug() << message;
}
socket.h
#ifndef SOCKET_H
#define SOCKET_H
#include <nzmqt/nzmqt.hpp>
#include <QByteArray>
#include <QDateTime>
#include <QList>
#include <QTimer>
#include <QThread>
namespace nzmqt
{
class Socket : public QThread
{
Q_OBJECT
public:
Socket(QObject *parent=0);
~Socket();
void run();
public slots:
void stop();
void sendString();
void send(QString message);
signals:
void socketStarted(QString signal);
protected slots:
void recvString(const QList<QByteArray>& message);
private:
QString message;
QString m_address;
QString r_address;
ZMQSocket* m_socket;
ZMQSocket* r_socket;
QTimer* timerout;
Socket *client;
};
}
#endif // SOCKET_H
socket.cpp
#include "socket.h"
#include <QByteArray>
#include <QList>
#include <QSignalSpy>
nzmqt::Socket::Socket(QObject* parent) :
QThread(parent)
{
m_address = "tcp://127.0.0.1:8887";
ZMQContext* context = createDefaultContext(this);
context->start();
m_socket = context->createSocket(ZMQSocket::TYP_PUSH);
connect(m_socket, SIGNAL(connected()), SLOT(sendString()));
r_address = "tcp://127.0.0.1:8886";
r_socket = context->createSocket(ZMQSocket::TYP_SUB);
connect(r_socket, SIGNAL(recvString(const QList<QByteArray>&)), SLOT(recvString(const QList<QByteArray>&)));
timerout = new QTimer(this);
timerout->setInterval(2500);
timerout->setSingleShot(true);
connect(timerout, SIGNAL(timeout()), this, SLOT(timerout_timeout()));
client = new nzmqt::Socket(this);
client->start();
}
nzmqt::Socket::~Socket()
{
client->stop();
}
void nzmqt::Socket::run()
{
m_socket->connectTo(m_address); //send
r_socket->connectTo(r_address); //feedback
timerout->start();
this->exec();
}
void nzmqt::Socket::stop()
{
timerout->stop();
m_socket->close();
this->quit();
}
void nzmqt::Socket::send(QString message) //taking an emitted string from a button on the app
{
this->message = message;
}
void nzmqt::Socket::sendString()
{
m_socket->sendMessage(signal.toStdString().c_str());
emit socketStarted(signal);
QTimer::singleShot(0, this, SLOT(stop()));
}
void nzmqt::Socket::recvString(const QList<QByteArray>& message) //print feedback
{
qDebug() << message;
}
socket.h
#ifndef SOCKET_H
#define SOCKET_H
#include <nzmqt/nzmqt.hpp>
#include <QByteArray>
#include <QDateTime>
#include <QList>
#include <QTimer>
#include <QThread>
namespace nzmqt
{
class Socket : public QThread
{
Q_OBJECT
public:
Socket(QObject *parent=0);
~Socket();
void run();
public slots:
void stop();
void sendString();
void send(QString message);
signals:
void socketStarted(QString signal);
protected slots:
void recvString(const QList<QByteArray>& message);
private:
QString message;
QString m_address;
QString r_address;
ZMQSocket* m_socket;
ZMQSocket* r_socket;
QTimer* timerout;
Socket *client;
};
}
#endif // SOCKET_H