zeromqテスト

4503 ワード

debian
sudo apt-get install libzmq3-dev

 
pip install zmq

 
client.py
#coding: utf-8
import zmq

context = zmq.Context()
sock = context.socket(zmq.REQ)
sock.connect("tcp://127.0.0.1:55555")
sock.send_unicode("asdasd")
print(sock.recv_unicode())

 
server.py
#coding: utf-8
import zmq

context = zmq.Context()
sock = context.socket(zmq.REP)
sock.bind("tcp://127.0.0.1:55555")

while True:
    message = sock.recv_unicode()
    sock.send_unicode(message)

 
client.rb
require 'ffi-rzmq'

context = ZMQ::Context.new(1)
client_socket = context.socket(ZMQ::REQ)
client_socket.connect("tcp://127.0.0.1:5555")

client_socket.send_string("121212")

loop do
  message = ""
  return_code = client_socket.recv_string message
  if return_code <= 0
    next
  end

  puts "client get #{message}"
  client_socket.send_string message
end

 
server.rb
require 'ffi-rzmq'

context = ZMQ::Context.new(1)
server_socket = context.socket(ZMQ::REP)
server_socket.bind("tcp://*:5555")

loop do
  message = ""
  return_code = server_socket.recv_string(message)
  if return_code <= 0
    next
  end

  puts "server get #{message}"
  server_socket.send_string(message)
end

 
githubのruby例を添付します
https://github.com/vishnugopal/zeromq-demo