Skip to content

Instantly share code, notes, and snippets.

@dnaeon
Created June 30, 2015 09:49
Show Gist options
  • Save dnaeon/2b5da5358bdcdf115cef to your computer and use it in GitHub Desktop.
Save dnaeon/2b5da5358bdcdf115cef to your computer and use it in GitHub Desktop.
rust-libzmq-helloworld
extern crate libc;
extern crate libzmq;
use std::ffi;
use std::thread;
unsafe fn helloworld_client() {
let context = libzmq::zmq_ctx_new();
let receiver = libzmq::zmq_socket(context, libzmq::ZMQ_REQ as libc::c_int);
let endpoint = ffi::CString::new("tcp://127.0.0.1:5555").unwrap();
assert_eq!(libzmq::zmq_connect(receiver, endpoint.as_ptr()), 0);
let data = ffi::CString::new("Hello").unwrap();
let buffer = ffi::CString::new("").unwrap();
for request in 0..10 {
println!("Sending request #{}", request);
libzmq::zmq_send(receiver, data.as_ptr() as *const libc::c_void, 5, 0);
thread::sleep_ms(1000);
libzmq::zmq_recv(receiver, buffer.as_ptr() as *mut libc::c_void, 5, 0);
println!("Received reply!");
}
}
fn main() {
unsafe { helloworld_client(); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment