体育资讯网

您现在的位置是:首页 > 分类11 > 正文

分类11

zmqsend源码(zmq 协议)

hacker2022-06-12 16:05:14分类1157
本文目录一览:1、windows下怎么使用zeromq

本文目录一览:

windows下怎么使用zeromq

Windows下VS2008使用ZeroMQ说明

一、下载ZeroMQ

二、编译ZeroMQ库文件

解压zeromq-4.0.3.zip文件,进入builds\msvc目录,用VS打开*.sln工程文件,编译生成解决方案。编译完成后,会在lib目录下生成dll和lib文件

三、编写简单的测试工程

用VS新建2个项目,一个是server端,一个是client端

将ZeroMQ源码项目的include目录下的两个文件“zmq.h”,“zmq_utils.h”拷贝至自己新建的工程

将ZeroMQ源码项目的lib目录下的两个文件“libzmq.dll”,“libzmq.lib”拷贝至自己新建的工程

将文件“zmq.h”,“zmq_utils.h”和“libzmq.lib”添加进自己新建的项目。

client端代码:

#include stdio.h

#include iostream

#include string.h

#include "zeroMQ/zmq.h"

#include "zeroMQ/zmq_utils.h"

int main(int argc,char** argv)

{

//zeroMQ相关变量

void * m_context;

void * m_subscriber;

char   m_pubAddr[64];

m_context = zmq_init (1);

m_subscriber = zmq_socket (m_context, ZMQ_SUB);

//订阅

char * puberIP = "127.0.0.1";

WORD puberPort = 8585;

memset(m_pubAddr,0,sizeof(m_pubAddr));

sprintf(m_pubAddr,"tcp://%s:%d",puberIP,puberPort);

//zmq_connect (m_subscriber, "tcp://localhost:5556");

zmq_connect (m_subscriber, m_pubAddr);

int ret = zmq_setsockopt (m_subscriber, ZMQ_SUBSCRIBE, NULL, 0);

while(1)

{

BYTE buf[1024] = {0};

DWORD buflen = sizeof(buf);

DWORD gotLen = zmq_recv(m_subscriber,buf,buflen,0);

printf("收到发布信息:%s\n",buf);

}

zmq_close (m_subscriber);

zmq_term (m_context);

return 0;

}

6.server端代码:

#includestdio.h

#includestring.h

#includestdlib.h

#include "zeroMQ/zmq.h"

#include "zeroMQ/zmq_utils.h"

int main(int argc,char** argv)

{

char pubLocalAddr[64] = {0};

sprintf(pubLocalAddr,"tcp://*:%d",8585);

//zeroMQ相关变量

void * m_context;

void * m_publisher;

m_context   = zmq_init (1);

m_publisher = zmq_socket (m_context, ZMQ_PUB);

zmq_bind (m_publisher, pubLocalAddr);

char *msg = "1231321";

while(1)

{

DWORD sendlen = zmq_send(m_publisher,msg,strlen(msg),0);

printf("发布信息:%s\n",msg);

Sleep(1000);

}

zmq_close (m_publisher);

zmq_term (m_context);

return 1;

}

用python写一个zmq的client和server

给你一个示例程序,你看了就知道怎么做了。

服务端:

#  

#   Hello World server in Python  

#   Binds REP socket to tcp://*:5555  

#   Expects "Hello" from client, replies with "World"  

#  

import zmq  

import time  

  

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: ", message  

  

    #  Do some 'work'  

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

  

    #  Send reply back to client  

    socket.send("World")

客户端:

#  

#   Hello World client in Python  

#   Connects REQ socket to tcp://localhost:5555  

#   Sends "Hello" to server, expects "World" back  

#  

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 (1,10):  

    print "Sending request ", request,"..."  

    socket.send ("Hello")  

      

    #  Get the reply.  

    message = socket.recv()  

    print "Received reply ", request, "[", message, "]"

zeromq java 怎么使用安装

首先

1.下载最新版的zeromq

2 解压

tar -xvf zeromq-3.1.0-beta.tar.gz

3 运行configure

./configure --prefix=/data/zeromq (prefix 指定安装目录)

4. make

5. make install

6. 设置环境变量

export CPPFLAGS=-I/home/mine/0mq/include/

export LDFLAGS=-L/home/mine/0mq/lib/

7.测试代码

server.c

#######################################

#include /data/zeromq/include/zmq.h

#include stdio.h

#include unistd.h

#include string.h

#include stdlib.h

int main (void)

{

void *context = zmq_init (1);

// Socket to talk to clients

void *responder = zmq_socket (context, ZMQ_REP);

zmq_bind (responder, "tcp://192.168.0.185:5555");

printf("binding on port 5555.\nwaiting client send message...\n");

while (1) {

// Wait for next request from client

zmq_msg_t request;

zmq_msg_init (request);

char buf[32];

zmq_recv (responder,buf, request, 0);

int size = zmq_msg_size (request);

char *string = malloc (size + 1);

memset(string,0,size+1);

memcpy (string, zmq_msg_data (request), size);

printf ("Received Hello string=[%s]\n",string);

free(string);

zmq_msg_close (request);

// Do some 'work'

sleep (1);

// Send reply back to client

zmq_msg_t reply;

char res[128]={0};

snprintf(res,127,"reply:%d",random());

zmq_msg_init_size (reply, strlen(res));

memcpy (zmq_msg_data (reply), res, strlen(res));

char buf2[32];

zmq_send (responder, buf2, reply, 0);

zmq_msg_close (reply);

}

// We never get here but if we did, this would be how we end

zmq_close (responder);

zmq_term (context);

return 0;

}

########################################################

#client.c

#include zmq.h

#include string.h

#include stdio.h

#include unistd.h

int main ()

{

void *context = zmq_init (1); // Socket to talk to server

printf ("Connecting to hello world server...\n");

void *requester = zmq_socket (context, ZMQ_REQ);

zmq_connect (requester, "tcp://192.168.0.185:5555");

int request_nbr;

for (request_nbr = 0; request_nbr != 10; request_nbr++)

{

zmq_msg_t request;

zmq_msg_init_data (request, "Hello", 6, NULL, NULL);

printf ("Sending request %d...\n", request_nbr);

zmq_send (requester, request, 0,0);

printf("send over") ;

zmq_msg_close (request);

zmq_msg_t reply;

zmq_msg_init (reply);

zmq_recv (requester, reply, 0,0);

printf ("Received reply %d: [%s]\n", request_nbr, (char *) zmq_msg_data (reply));

zmq_msg_close (reply);

}

zmq_close (requester);

zmq_term (context);

return 0;

}

gcc server.c -o server -lzmq -L/data/zeromq/lib -I/data/zeromq/include

gcc client.c -o client -lzmq -L/data/zeromq/lib -I/data/zeromq/include

./server

./client

jzmq 是zeromq 的java客户端

下载地址

1. tar xzf zeromq-jzmq-semver-90-g58c6108.tar.gz

2. ./configure

这个时候需要上面环境变量的配置,

还需要

随便

vi /data/zeromq/.bashrc

export PATH=$PATH:/data/zeromq/include

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/zeromq/lib:/data/zeromq/include

只要configure 执行完毕就会生成makefile文件

3.make

4.make install

测试

hwserver.java

//

// Hello World server in Java

// Binds REP socket to tcp://*:5555

// Expects "Hello" from client, replies with "World"

//

// Naveen Chawla naveen.chwl@gmail.com

//

import org.zeromq.ZMQ;

public class hwserver {

public static void main(String[] args) {

// 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 a 0-terminated string (C string) from the client,

// so that this server also works with The Guide's C and C++ "Hello World" clients

request = socket.recv (0);

// In order to display the 0-terminated string as a String,

// we omit the last byte from request

System.out.println ("Received request: [" +

new String(request,0,request.length-1) // Creates a String from request, minus the last byte

+ "]");

// Do some 'work'

try {

Thread.sleep (1000);

}

catch(InterruptedException e){

e.printStackTrace();

}

// Send reply back to client

// We will send a 0-terminated string (C string) back to the client,

// so that this server also works with The Guide's C and C++ "Hello World" clients

String replyString = "World" + " ";

byte[] reply = replyString.getBytes();

reply[reply.length-1]=0; //Sets the last byte of the reply to 0

socket.send(reply, 0);

}

}

}

hwclient.java

//

// Hello World client in Java

// Connects REQ socket to tcp://localhost:5555

// Sends "Hello" to server, expects "World" back

//

// Naveen Chawla naveen.chwl@gmail.com

//

import org.zeromq.ZMQ;

public class hwclient{

public static void main(String[] args){

// Prepare our context and socket

ZMQ.Context context = ZMQ.context(1);

ZMQ.Socket socket = context.socket(ZMQ.REQ);

System.out.println("Connecting to hello world server...");

socket.connect ("tcp://localhost:5555");

// Do 10 requests, waiting each time for a response

for(int request_nbr = 0; request_nbr != 10; request_nbr++) {

// Create a "Hello" message.

// Ensure that the last byte of our "Hello" message is 0 because

// our "Hello World" server is expecting a 0-terminated string:

String requestString = "Hello" + " ";

byte[] request = requestString.getBytes();

request[request.length-1]=0; //Sets the last byte to 0

// Send the message

System.out.println("Sending request " + request_nbr + "...");

socket.send(request, 0);

// Get the reply.

byte[] reply = socket.recv(0);

// When displaying reply as a String, omit the last byte because

// our "Hello World" server has sent us a 0-terminated string:

System.out.println("Received reply " + request_nbr + ": [" + new String(reply,0,reply.length-1) + "]");

}

}

}

javac -classpath /data/jzmq/share/java/zmq.jar -d . h*.java

java -Djava.library.path=/data/jzmq/lib -cp /data/jzmq/share/java/zmq.jar:. hwserver

java -Djava.library.path=/data/jzmq/lib -cp /data/jzmq/share/java/zmq.jar:. hwclient

显示内容:

server端

Received request: [Hello]

Received request: [Hello]

Received request: [Hello]

Received request: [Hello]

Received request: [Hello]

Received request: [Hello]

client端

Connecting to hello world server...

Sending request 0...

Received reply 0: [World]

Sending request 1...

Received reply 1: [World]

Sending request 2...

Received reply 2: [World]

Sending request 3...

Received reply 3: [World]

Sending request 4...

Received reply 4: [World]

Sending request 5...

发表评论

评论列表

  • 假欢笑惜(2022-06-13 02:11:03)回复取消回复

    r, ZMQ_SUBSCRIBE, NULL, 0);while(1){BYTE buf[1024] = {0};DWORD buflen = sizeof(buf);DWORD 

  • 礼忱谷夏(2022-06-12 18:43:25)回复取消回复

    p://localhost:5555"); // Do 10 requests, waiting each time for a response for(int request_nbr = 0;