1

如果收到任何数据,是否可以将 QSocketNotifier 用于 nanomsg 套接字以执行某些操作?我尝试使用此代码,但运行时没有任何反应nanocat --req --connect ipc:///tmp/node0.ipc --data pong --format ascii。我什至不知道如何检查问题发生在哪一步,因为没有错误。

Wrapper::Wrapper(QObject *parent) : QObject(parent) {
    ...
    createNode();
    int fd;
    size_t sz = sizeof(fd);
    nn_getsockopt(sock, NN_SOL_SOCKET, NN_RCVFD, &fd, &sz);
    QSocketNotifier m_notifier(fd, QSocketNotifier::Read);
    QObject::connect(&m_notifier, SIGNAL(activated(int)), this,   SLOT(nmsgRecieve()));
    m_notifier.setEnabled(true);
    ...
}

void Wrapper::createNode() {
    const char* url = "ipc:///tmp/node0.ipc";

    if ((sock = nn_socket(AF_SP, NN_REP)) < 0) {
            qDebug() << "nn_socket" << nn_strerror(nn_errno());
            exit(1);
    }
    if ((rv = nn_bind(sock, url)) < 0) {
            qDebug() << "nn_bind" << nn_strerror(nn_errno());
            exit(1);
    }
}

void Wrapper::nmsgRecieve() {
    qDebug() << "Some msg";
    char *buf = NULL;
    int bytes;
    if ((bytes = nn_recv(sock, &buf, NN_MSG, 0)) < 0) {
        qDebug() << "nn_recv" << nn_strerror(nn_errno());
        exit(1);
    }
    qDebug() << buf;
    nn_freemsg(buf);
 }
4

1 回答 1

0

嗯,这是一个非常愚蠢的问题,与 nanomsg 或 QSocketNotifier 无关。我创建了我的 QSocketNotifer,以便它在构造函数块结束后立即被销毁。

于 2018-07-18T12:13:15.863 回答