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サーバー」をサーバー起動し、
2) 「testZMQ.exe」で Enter キーを押したままにして、多数のクライアントを起動します。
サーバーを複数回起動してみてください: このバグは、サーバーが 2 回以上起動したときに発生すると思います。
#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;
}
私は何を間違っていますか?