【问题标题】:zeromq socket recv hangingzeromq 套接字接收挂起
【发布时间】:2013-07-16 20:55:25
【问题描述】:

我使用 zeromq(最新版本,2012.04.04)、c++、ms vs 2008(x86 发布/调试)、windows 7 x64。我尝试编写简单的客户端-服务器系统。

问题

我的问题是在 Windows 7 上的 recv() 命令中计算机死机许多客户端同时连接到服务器

在 Windows Vista 上,结果就像在 Windows XP 上一样 - 客户端失败:
http://research.makseq.com/testZMQ/2.Vista.png
http://research.makseq.com/testZMQ/1.PNG
失败后客户端无法连接到服务器,他们写道:
“断言失败:地址已在使用中:...”

这里是源代码:

http://research.makseq.com/testZMQ/testZMQ.rar

使用方法:

1) "testZMQ.exe server" 到服务器启动,

2) 按回车并按住“testZMQ.exe”启动多对多客户端。

尝试多次启动服务器:我认为当服务器启动两次或更多次时会发生此错误。

#include "stdafx.h"
#include "../Libraries/zeromq/include/zmq.h"
#include "../Libraries/zeromq/include/zhelpers.hpp"
#include <string>
using namespace std; 

#ifdef WIN32
#pragma comment(lib, "../Libraries/zeromq/libzmq.lib") 
#endif

//-----------------------------------------------------------------------------
int server()
{
    cout << ":: Server ::" << endl;
    zmq::context_t context(1);
    zmq::socket_t server(context, ZMQ_REP);
    server.bind("tcp://*:7774");

    while (1) {

         // receive 
         zmq::message_t messageR;
         cout << "debug point 1" << endl; 
         server.recv(&messageR); // <== dead hanging here
         cout << "debug point 2" << endl; 
         string recieved = string(static_cast<char*>(messageR.data()), messageR.size());

         // send
         string reply = "do something";
         zmq::message_t messageS(reply.size());
         memcpy(messageS.data(), reply.data(), reply.size()); 
         cout << "debug point 3" << endl; 
         server.send(messageS);
         cout << "debug point 4" << endl; 

    }
    return 0;
}

//-----------------------------------------------------------------------------
int client()
{
    cout << ":: Client ::" << endl;
    // connect
    zmq::context_t context(1);
    zmq::socket_t *client = new zmq::socket_t (context, ZMQ_REQ);
    client->connect("tcp://localhost:7774");
    int linger = 0;
    client->setsockopt (ZMQ_LINGER, &linger, sizeof (linger));

    // send
    string reply = "hello";
    zmq::message_t messageS(reply.size());
    memcpy(messageS.data(), reply.data(), reply.size()); 
    client->send(messageS);

    // receive
    zmq::message_t messageR;
    client->recv(&messageR);
    string recieved = string(static_cast<char*>(messageR.data()), messageR.size());

    // close
    client->close();
    delete client;
    zmq_term(&context);

    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    if (argc == 2) server(); 
    else client(); 

    return 0;
}

我做错了什么?

【问题讨论】:

    标签: c++ zeromq freeze


    【解决方案1】:

    recv 是一个阻塞调用。在某些东西连接之前,它会坐在那里等待。这不是死机,它只是在等待连接。

    【讨论】:

    • m.. 我什么都做不了:移动鼠标,启动任何程序或关闭我的程序。我的电脑完全挂了..
    • 哇,这听起来比 ZMQ 正在做的事情要大得多。你在另一台机器上试过同样的事情吗?您对相同的编译二进制文件有同样的问题吗?你能发布你的完整源代码吗?
    • 是的,我在另一台机器上试过这个东西!结果是一样的:全挂了!问题在于使用不同的构建:x86 调试、x86 发布...
    • 谢谢。我回家后会试一试。顺便说一句,您是否尝试过绑定到单个接口/IP 地址而不是通配符以查看是否有效?
    • 我已经在 Window XP 上测试过了。没有死机,但客户端的错误是:research.makseq.com/testZMQ/1.PNG
    【解决方案2】:

    “地址已在使用”错误似乎意味着您对 socket.bind() 的调用发生了多次 - 即您正在运行多个服务器进程。您只能绑定一次到给定接口上的给定端口。

    我怀疑这解释了为什么它会导致机器挂起......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多