Zeromq的Python和Rust简易实现

python 环境:

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

服务端代码:

#hwserver.py
import time
import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")

while True:
    #  Wait for next request from client
    message = socket.recv()
    print("Received request: %s" % message)

    #  Do some 'work'
    time.sleep(1)

    #  Send reply back to client
    socket.send(b"World")

客户端代码:

import zmq

context = zmq.Context()

#  Socket to talk to server
print("Connecting to hello world server...")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")

#  Do 10 requests, waiting each time for a response
for request in range(10):
    print("Sending request %s ..." % request)
    socket.send(b"Hello")

    #  Get the reply.
    message = socket.recv()
    print("Received reply %s [ %s ]" % (request, message))

Rust环境:


rustc 1.15.0-nightly (28d6623bc 2016-12-03)

服务端代码:

//hwserver.rs
//需要apt-get install libzmq3-dev
//然后在Cargo.toml加入:
//[dependencies.libc]
//version = "*"

//[dependencies.libzmq]
//git = "https://github.com/dnaeon/rust-libzmq.git"


extern crate libc;
extern crate libzmq;

use std::ffi;

unsafe fn helloworld_server() {
    let context = libzmq::zmq_ctx_new();
    let responder = libzmq::zmq_socket(context, libzmq::ZMQ_REP as libc::c_int);

    let endpoint = ffi::CString::new("tcp://*:5555").unwrap();
    assert_eq!(libzmq::zmq_bind(responder, endpoint.as_ptr()), 0);

    let buffer = ffi::CString::new("Hello").unwrap();
    let data = ffi::CString::new("World").unwrap();

    loop {
        libzmq::zmq_recv(responder, buffer.as_ptr() as *mut libc::c_void, 5, 0);
        println!("Received Hello!");
        libzmq::zmq_send(responder, data.as_ptr() as *const libc::c_void , 5, 0);
    }
}

fn main() {
    unsafe { helloworld_server(); }
}

客户端代码:

//hwclient.rs
//需要apt-get install libzmq3-dev
//然后在Cargo.toml加入:
//[dependencies.libc]
//version = "*"

//[dependencies.libzmq]
//git = "https://github.com/dnaeon/rust-libzmq.git"
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(); }
}

你可能感兴趣的:(python,zeromq,Rust)