问题
I use zeromq (latest version, 2012.04.04), c++, ms vs 2008 (x86 release/debug), windows 7 x64. I try to code simple client-server system.
Problem
My problem is dead hanging of the computer at recv() command on Windows 7, when many clients connect to the server at the same time.
On Windows Vista the result is like on Windows XP - client has the fail:
http://research.makseq.com/testZMQ/2.Vista.png
http://research.makseq.com/testZMQ/1.PNG
After this fail clients can not connect to the server, they write:
"Assertion fail: Address already in use: ..."
Here is source code:
http://research.makseq.com/testZMQ/testZMQ.rar
How to use:
1) "testZMQ.exe server" to server start,
2) press enter and hold at "testZMQ.exe" to start many-many-many clients.
Try to start the server multiple times: I think this bug happens when the server started two and more times.
#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;
}
What do I do wrong?
回答1:
recv
is a blocking call. Until something connects, it will sit there and wait. It's not a dead hang, it's just waiting for a connection.
回答2:
The "Address already in use" error seems to imply your call to socket.bind() is happening multiple times - i.e. you're running more than one server process. You can only bind once to a given port on a given interface.
I doubt that explains why it causes the machine to hang though...
来源:https://stackoverflow.com/questions/10258867/zeromq-socket-recv-hanging