Something is not quite right, as the ruby client hangs after final request, but it's a start.
This commit is contained in:
parent
9a9e9ad551
commit
fc2f46f05a
1
zeromq/.gitignore
vendored
Normal file
1
zeromq/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
hwserver
|
6
zeromq/Makefile
Normal file
6
zeromq/Makefile
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
CFLAGS += -I. -I/usr/local/include
|
||||||
|
LDFLAGS += -lstdc++ -lpthread -luuid -lrt
|
||||||
|
|
||||||
|
|
||||||
|
hwserver: hwserver.c /usr/local/lib/libzmq.a
|
||||||
|
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
|
24
zeromq/hwclient.rb
Normal file
24
zeromq/hwclient.rb
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# vim:fileencoding=utf-8
|
||||||
|
|
||||||
|
require 'zmq'
|
||||||
|
|
||||||
|
def main
|
||||||
|
context = ZMQ::Context.new(1)
|
||||||
|
|
||||||
|
puts "Connecting to hello world server…"
|
||||||
|
requester = context.socket(ZMQ::REQ)
|
||||||
|
requester.connect("tcp://localhost:5555")
|
||||||
|
|
||||||
|
0.upto(9) do |request_nbr|
|
||||||
|
puts "Sending request #{request_nbr}…"
|
||||||
|
requester.send("Hello")
|
||||||
|
|
||||||
|
reply = requester.recv(6)
|
||||||
|
puts "Received reply #{request_nbr}: [#{reply}]"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
if $0 == __FILE__
|
||||||
|
main
|
||||||
|
end
|
34
zeromq/hwserver.c
Normal file
34
zeromq/hwserver.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#include <zmq.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
void *context = zmq_init(1);
|
||||||
|
|
||||||
|
void *responder = zmq_socket(context, ZMQ_REP);
|
||||||
|
printf("Binding server to tcp://*:5555\n");
|
||||||
|
zmq_bind(responder, "tcp://*:5555");
|
||||||
|
|
||||||
|
printf("Starting loop.\n");
|
||||||
|
while(1) {
|
||||||
|
zmq_msg_t request;
|
||||||
|
zmq_msg_init(&request);
|
||||||
|
zmq_recv(responder, &request, 0);
|
||||||
|
printf("Received Hello\n");
|
||||||
|
zmq_msg_close(&request);
|
||||||
|
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
zmq_msg_t reply;
|
||||||
|
zmq_msg_init_size(&reply, 5);
|
||||||
|
memcpy(zmq_msg_data(&reply), "World", 5);
|
||||||
|
zmq_send(responder, &reply, 0);
|
||||||
|
zmq_msg_close(&reply);
|
||||||
|
}
|
||||||
|
|
||||||
|
zmq_close(responder);
|
||||||
|
zmq_term(context);
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user