zeromq套接字recv挂起 我使用ZrOMQ(最新版本,2012.04.04),C++,MS vs 2008(X86发布/调试),Windows 7 X64。我尝试编写简单的客户机-服务器系统代码 问题
我的问题是,在Windows 7上,当许多客户端同时连接到服务器时,计算机会死机 在Windows Vista上,结果与在Windows XP上类似-客户端出现故障:zeromq套接字recv挂起 我使用ZrOMQ(最新版本,2012.04.04),C++,MS vs 2008(X86发布/调试),Windows 7 X64。我尝试编写简单的客户机-服务器系统代码 问题,c++,zeromq,freeze,C++,Zeromq,Freeze,我的问题是,在Windows 7上,当许多客户端同时连接到服务器时,计算机会死机 在Windows Vista上,结果与在Windows XP上类似-客户端出现故障: 发生此故障后,客户端无法连接到服务器,他们会写入: “断言失败:地址已在使用中:…” 以下是源代码: 如何使用: 1) “testZMQ.exe服务器”以启动服务器 2) 按enter键并按住“testZMQ.exe”以启动多个客户端 尝试多次启动服务器:我认为当服务器启动两次或更多次时,就会出现此错误 #include "
发生此故障后,客户端无法连接到服务器,他们会写入:
“断言失败:地址已在使用中:…” 以下是源代码: 如何使用: 1) “testZMQ.exe服务器”以启动服务器 2) 按enter键并按住“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;
}
#包括“stdafx.h”
#包括“./Libraries/zeromq/include/zmq.h”
#包括“./Libraries/zeromq/include/zhelpers.hpp”
#包括
使用名称空间std;
#ifdef WIN32
#pragma注释(lib,“../Libraries/zeromq/libzmq.lib”)
#恩迪夫
//-----------------------------------------------------------------------------
int服务器()
{
coutrecv
是一个阻塞调用。在有东西连接之前,它会一直坐在那里等待。这不是一个死挂起,它只是在等待一个连接。这个“地址已在使用”错误似乎暗示了您对socket.bind()的调用多次发生-即,您正在运行多个服务器进程。您只能绑定一次给定接口上的给定端口
我怀疑这是否能解释为什么它会导致机器挂起…m..我什么都做不了:移动鼠标、启动任何程序或关闭我的程序。我的计算机完全挂起了..哇,这听起来比ZMQ正在做的事情更严重。你在另一台机器上尝试过同样的事情吗?你对同一个com有同样的问题吗堆积的二进制文件?你能发布完整的源代码吗?是的,我在另一台机器上尝试过这个方法!结果是一样的:完全挂起!问题在于不同的版本:x86调试,x86发行版…谢谢。我回家后会对此进行解释。顺便问一句,你是否尝试过绑定到单个接口/IP地址而不是wildcard是否能正常工作?我已经在Windows XP上进行了测试。没有死机,但客户端的错误是: