Skip to content
Snippets Groups Projects
Commit 8b94e3d9 authored by Manuela Kuhn's avatar Manuela Kuhn
Browse files

Tested zmq with c and c++

parent d054f821
No related branches found
No related tags found
No related merge requests found
send: send.cpp
g++ -o send send.cpp -lzmq -std=c++0x
send_c: send.c
gcc -o send_c send.c -lzmq
import zmq
import sys
import json
port = "6000"
ip="localhost"
#ip="*"
#ip="zitpcx19282.desy.de"
context = zmq.Context()
print "Connecting to server..."
socket = context.socket(zmq.REQ)
socket.connect ("tcp://"+ ip + ":%s" % port)
# Do 10 requests, waiting each time for a response
for request in range (1,10):
# Send request
socket.send("test")
print "send request"
# Get the reply.
message = socket.recv()
print "received reply ", request, "[", message, "]"
print "as json: ", json.loads(message)
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
void cleanup(void* socket, void* context)
{
zmq_close(socket);
zmq_ctx_destroy(context);
}
int main () {
char* signalHost = "*";
char* signalPort = "6000";
// Prepare our context and socket
void *context = zmq_ctx_new();
void *socket = zmq_socket(context, ZMQ_REP);
// char* connectionStr;
char connectionStr[128];
char* filename = "/space/projects/zeromq-data-transfer/data/source/local/5.cbf";
int i = 0;
// char* m;
char m[128];
char request[128];
sprintf(connectionStr, "tcp://%s:%s", signalHost, signalPort);
int rc = zmq_bind(socket, connectionStr);
printf("Sender: Started\n");
while (1)
{
printf("Receiving");
// Wait for next request from client
int num = zmq_recv(socket, request, strlen(request), 0);
if (num > 0)
{
request[num] = '\0';
printf("Receiver: Received (%s)\n", request);
}
// Do some 'work'
sleep(1);
// Send reply back to client
sprintf(m, "{ \"filePart\": %d, \"filename\": \"%s\" }", i, filename);
// sprintf(m, "{ \"filePart\": %d, \"filename\": \"/space/projects/zeromq-data-transfer/data/source/local/5.cbf\" }", i);
printf("Sender: Sending (%s)\n", m);
int rc = zmq_send(socket, m, strlen(m), 0);
printf("Sended\n");
i++;
}
zmq_close(socket);
zmq_ctx_destroy(context);
return 0;
}
//
// 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 () {
std::string signalHost = "*";
std::string signalPort = "6000";
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REP);
std::string connectionStr("tcp://" + signalHost + ":" + signalPort);
socket.bind (connectionStr.c_str());
// socket.bind ("tcp://*:6000");
int i = 0;
// char m[7];
std::string filename = "/space/projects/zeromq-data-transfer/data/source/local/5.cbf";
std::string m;
while (true) {
zmq::message_t request;
// Wait for next request from client
socket.recv (&request);
std::cout << "Received: " << static_cast<char*>(request.data()) << std::endl;
// Do some 'work'
sleep(1);
// Send reply back to client
m = "{ \"filePart\": " + std::to_string(i) + ", \"filename\": \"" + filename + "\" }";
// sprintf(m, "world_%d", i);
// std::cout << "sizeof(m) " << sizeof(m) << ", m " << m << std::endl;
zmq::message_t reply (m.length());
memcpy ( reply.data (), m.c_str(), m.length());
socket.send (reply);
i++;
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment