ZeroMQ-J

本文介绍了一个使用ZeroMQ实现的请求/响应模式通信示例。包括服务端与客户端的代码实现,以及如何通过ZeroMQ框架进行网络通信的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ZeroMQ是一个网络通信框架.
相关帮助文档地址: [url]http://zguide.zeromq.org/page:all#Handling-Multiple-Sockets[/url]

通信模式示意:Request/Response

ON SERVER

server端的代码:

package test;

import org.zeromq.ZMQ;

public class Response {
public static void main (String[] args) {

ZMQ.Context context = ZMQ.context(1); //创建用于一个I/O线程的context

ZMQ.Socket socket = context.socket(ZMQ.REP); //创建一个response类型的socket,服务端接受Request.
socket.bind ("tcp://*:5555"); //端口绑定
int i = 0;
int number = 0;
while (!Thread.currentThread().isInterrupted()) {
i++;
if (i == 10000) {
i = 0;
System.out.println(++number);
}
byte[] request = socket.recv(); //获取request数据
//System.out.println("receive : " + new String(request));
String response = "world";
socket.send(response.getBytes()); //向request端发送数据 ,必须要要request端返回数据,没有返回就又recv,将会出错,这里可以理解为强制要求走完整个request/response流程
}
socket.close(); //关闭socket
context.term(); //关闭当前的上下文

}
}



客户端


package test;

import org.zeromq.ZMQ;

public class Request {
public static void main(String args[]) {
for (int j = 0; j < 5; j++) {
new Thread(new Runnable(){

public void run() {
// TODO Auto-generated method stub
ZMQ.Context context = ZMQ.context(1); //创建一个I/O线程的上下文
ZMQ.Socket socket = context.socket(ZMQ.REQ); //客户端,用于向response端发送数据

socket.connect("tcp://127.0.0.1:5555"); //与response端建立连接
long now = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
String request = "hello";
socket.send(request.getBytes()); //向reponse端发送数据
byte[] response = socket.recv(); //接收response发送回来的数据 正在request/response模型中,send之后必须要recv之后才能继续send,这可能是为了保证整个request/response的流程走完
// System.out.println("receive : " + new String(response));
}
long after = System.currentTimeMillis();

System.out.println((after - now) / 1000);
}

}).start();;
}

}
}



创建了包含一个线程的context,然后创建了一个REQ,也就是request类型的socket,然后与5555端口建立连接,然后就进入了循环,不断的向服务端发送数据,然后接收数据。。。

Maven 相关依赖配置
<dependency>
<groupId>org.zeromq</groupId>
<artifactId>jeromq</artifactId>
<version>0.3.1</version>
</dependency>
用中文分析错误原因,给出解决方法cd zeromq-4.3.5/ [root@localhost:2 zeromq-4.3.5]# ./autogen.sh autoreconf: Entering directory `.' autoreconf: configure.ac: not using Gettext autoreconf: running: aclocal -I config --force -I config autoreconf: configure.ac: tracing autoreconf: running: libtoolize --copy --force libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, 'config'. libtoolize: copying file 'config/ltmain.sh' libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'config'. libtoolize: copying file 'config/libtool.m4' libtoolize: copying file 'config/ltoptions.m4' libtoolize: copying file 'config/ltsugar.m4' libtoolize: copying file 'config/ltversion.m4' libtoolize: copying file 'config/lt~obsolete.m4' autoreconf: running: /usr/bin/autoconf --include=config --force configure.ac:28: error: possibly undefined macro: AC_SUBST If this token and others are legitimate, please use m4_pattern_allow. See the Autoconf documentation. configure.ac:74: error: missing some pkg-config macros (pkg-config package) configure.ac:132: error: possibly undefined macro: AC_MSG_RESULT configure.ac:145: error: possibly undefined macro: AC_DEFINE configure.ac:253: error: possibly undefined macro: AC_CHECK_LIB configure.ac:356: error: possibly undefined macro: AC_CHECK_HEADERS configure.ac:358: error: possibly undefined macro: AC_MSG_ERROR configure.ac:545: error: missing some pkg-config macros (pkg-config package) configure.ac:550: error: possibly undefined macro: AC_SEARCH_LIBS configure.ac:593: error: possibly undefined macro: AC_MSG_NOTICE configure.ac:888: error: possibly undefined macro: AC_MSG_WARN autoreconf: /usr/bin/autoconf failed with exit status: 1 autogen.sh: error: autoreconf exited with status 1
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值