server
// Hello World server
#include "stdafx.h"
#include <zmq.h>
#include <stdio.h>
#include <Windows.h>
#include <string.h>
#include <assert.h>
int main(void)
{
// Socket to talk to clients
void *context = zmq_ctx_new();
void *responder = zmq_socket(context, ZMQ_REP);
int rc = zmq_bind(responder, "tcp://*:5555");
assert(rc == 0);
while (1)
{
char buffer[10];
zmq_recv(responder, buffer, 10, 0);
printf("Received Hello\n");
Sleep(1); // Do so// zeroclient.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
Hello World server in C++
Binds REP socket to tcp://*:5555
Expects "Hello" from client, replies with "World"
//#include <zmq.hpp>
//#include <string>
//#include <iostream>
//#ifndef _WIN32
//#include <unistd.h>
//#else
//#include <windows.h>
//
//#define sleep(n) Sleep(n)
//#endif
//
//int main() {
// // Prepare our context and socket
// zmq::context_t context(1);
// zmq::socket_t socket(context, ZMQ_REP);
// socket.bind("tcp://*:5555");
//
// while (true) {
// zmq::message_t request;
//
// // Wait for next request from client
// socket.recv(&request);
// std::cout << "Received Hello" << std::endl;
//
// // Do some 'work'
// sleep(1);
//
// // Send reply back to client
// zmq::message_t reply(5);
// memcpy(reply.data(), "World", 5);
// socket.send(reply);
// }
// return 0;
//}
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <windows.h>
int main(void)
{
printf("Connecting to hello world server…\n");
void *context = zmq_ctx_new();
void *requester = zmq_socket(context, ZMQ_REQ);
zmq_connect(requester, "tcp://localhost:5555");
int request_nbr;
for (request_nbr = 0; request_nbr != 10; request_nbr++) {
char buffer[10];
printf("Sending Hello %d…\n", request_nbr);
zmq_send(requester, "Hello", 5, 0);
zmq_recv(requester, buffer, 10, 0);
printf("Received World %d\n", request_nbr);
}
zmq_close(requester);
zmq_ctx_destroy(context);
return 0;
}
me 'work'
zmq_send(responder, "World", 5, 0);
}
return 0;
}
client