×

zeromq

zeromq如何接收16进制数据?zeromq java 怎么使用安装

admin admin 发表于2022-06-09 10:03:50 浏览115 评论0

抢沙发发表评论

zeromq如何接收16进制数据


 转换方法:
  16进制就是逢16进1,但我们只有0~9这十个数字,所以我们用A,B,C,D,E,F这六个字母来分别表示10,11,12,13,14,15。字母不区分大小写。
  十六进制数的第0位的权值为16的0次方,第1位的权值为16的1次方,第2位的权值为16的2次方……,所以,在第N(N从0开始)位上,如果是是数 X (X 大于等于0,并且X小于等于 15,即:F)表示的大小为 X * 16的N次方。
  例如:
  用竖式计算:
  2AF5换算成10进制:
  第0位: 5 * 16的0次方 = 5
  第1位: F * 16的1次方 = 240
  第2位: A * 16的2次方 = 2560
  第3位: 2 * 16的3次方 = 8192
  直接计算就是:
  5 * 16的0次方 + F * 16的1次方 + A * 16的2次方 + 2 * 16的3次方 = 10997

zeromq java 怎么使用安装


首先
http://download.zeromq.org/
1.下载最新版的zeromq
http://download.zeromq.org/zeromq-3.1.0-beta.tar.gz
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;
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={0};
snprintf(res,127,“reply:%d“,random());
zmq_msg_init_size (&reply, strlen(res));
memcpy (zmq_msg_data (&reply), res, strlen(res));
char buf2;
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客户端

下载地址
https://github.com/zeromq/jzmq/downloads

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...

zeromq安装运行出现问题,请问该怎么解决


首先,运行vs2013,打开解决方案\zeromq-4.0.5\builds\msvc\msvc.sln,系统提示转换方案,选择是即可。设置配置为release win32。

执行“生成/生成解决方案”,没有意外的话构建会成功。

生成的libzmq.lib位于\zeromq-4.0.5\builds\msvc\Release,libzmq.dll则位于\zeromq-4.0.5\lib中,将libzmq.lib拷贝到\zeromq-4.0.5\lib目录,方便后面安装jzmq。
-zeromq