Created
September 19, 2012 20:43
-
-
Save davisford/3752142 to your computer and use it in GitHub Desktop.
zeromq bad address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <msgpack.h> | |
#include <zmq.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include "zhelpers.h" | |
#include <czmq.h> | |
#include <errno.h> | |
void check(int retCode, const char* fn, int lineNo); | |
void zeromq_request_reply(); | |
int main (int argc, char *argv[]) { | |
zeromq_request_reply(); | |
return 0; | |
} | |
// test error code from 0MQ API calls | |
void check(int retCode, const char* fn, int lineNo) | |
{ | |
if (retCode < 0) | |
{ | |
perror("ZMQ Error: \t"); | |
printf("%s (line %d) returned errno %s \n", fn, lineNo, strerror(errno)); | |
} | |
} | |
// test simple Hello World request/reply | |
void zeromq_request_reply() { | |
////// TEST THAT ZEROMQ WORKS OK | |
int major, minor, patch; | |
zmq_version(&major, &minor, &patch); | |
printf("\n\nCurrent 0MQ version is %d.%d.%d\n", major, minor, patch); | |
void *context = zmq_ctx_new(); | |
// socket to talk to server | |
printf("Connecting to hello world server ...\n"); | |
void *socket = zmq_socket (context, ZMQ_REQ); | |
zmq_connect(socket, "tcp://localhost:5555"); | |
int request_nbr; | |
// request/response with server in a loop | |
for (request_nbr = 0; request_nbr != 10; request_nbr++) { | |
zmq_msg_t request; | |
check(zmq_msg_init_size(&request, 5), "zmq_msg_init_size", 51); | |
memcpy(zmq_msg_data (&request), "Hello", 5); | |
printf("Sending Hello %d...\n", request_nbr); | |
check(zmq_msg_send(&request, socket, 0), "zmq_msg_send", 55); | |
check(zmq_msg_close(&request), "zmq_msg_close", 56); | |
zmq_msg_t reply; | |
check(zmq_msg_init(&reply), "zmq_msg_init", 59); | |
int size = zmq_msg_recv(&reply, socket, 0); | |
if (size < 0) { | |
perror("\nReceived zero bytes back\n"); | |
} else { | |
char *string = (char *) malloc(size+1); | |
memcpy(string, zmq_msg_data (&reply), size); | |
zmq_msg_close(&reply); | |
string[size] = 0; | |
printf("Received string[%d] len=%lu: %s \n", request_nbr, strlen(string), string); | |
} | |
check(zmq_msg_close(&reply), "zmq_msg_close", 73); | |
} | |
zmq_close(socket); | |
zmq_ctx_destroy(context); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.zeromq.ZMQ; | |
/** | |
* You must pass the following JVM arg: | |
* <tt>-Djava.library.path=/usr/local/lib</tt> | |
* or you'll have to specify the library environment variable for you platform: | |
* <tt>DYLD_LIBRARY_PATH</tt> for Mac or <tt>LD_LIBRARY_PATH</tt> for Linux | |
*/ | |
public class MainRunner { | |
public static void main(String[] args) { | |
System.out.println("ZeroMQ Version: "+ZMQ.getVersionString()); | |
// prepare our context and socket | |
ZMQ.Context context = ZMQ.context(1); | |
ZMQ.Socket socket = context.socket(ZMQ.REP); | |
socket.bind("tcp://*:5555"); | |
while (true) { | |
byte[] request; | |
// wait for next request from client | |
// we will wait for 0-terminated string (C-string) | |
// so that this server works with C/C++ clients | |
request = socket.recv(0); | |
// in order to display the 0-terminated string as a String | |
// we omit the last byte from the request | |
System.out.println("Received request: " + | |
String.format("[payload:%s, payload length:%d]", new String(request), request.length)); | |
// do some "work" | |
try { | |
Thread.sleep(1000); | |
} catch(InterruptedException e) { | |
e.printStackTrace(); | |
} | |
// send a reply back to client | |
// we will send a 0-terminated string (C-String) back | |
// so that this server works with C/C++ clients | |
String replyString = "World" + " "; | |
byte[] reply = replyString.getBytes(); | |
reply[reply.length-1] = 0; | |
socket.send(reply, 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment