#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <nanomsg/nn.h> #include <nanomsg/reqrep.h> #define SERVER_ADDRESS "tcp://127.0.0.1:5555" // 服务器地址 int main() { int sock = nn_socket(AF_SP, NN_REP); // 创建一个REP类型的socket if (sock < 0) { fprintf(stderr, "nn_socket error: %s\n", nn_strerror(nn_errno())); return -1; } if (nn_bind(sock, SERVER_ADDRESS) < 0) { // 绑定服务器地址 fprintf(stderr, "nn_bind error: %s\n", nn_strerror(nn_errno())); nn_close(sock); return -1; } printf("server started, waiting for client...\n"); while (1) { char *buf = NULL; int bytes = nn_recv(sock, &buf, NN_MSG, 0); // 接收客户端请求消息 if (bytes < 0) { fprintf(stderr, "nn_recv error: %s\n", nn_strerror(nn_errno())); nn_close(sock); return -1; } printf("server received: %s\n", buf); if (strcmp(buf, "123") == 0) { // 判断客户端请求消息是否为"123" char *response = "abc"; int response_len = strlen(response) + 1; int bytes = nn_send(sock, response, response_len, 0); // 发送回复消息 if (bytes < 0) { fprintf(stderr, "nn_send error: %s\n", nn_strerror(nn_errno())); nn_close(sock); return -1; } } else { printf("invalid request\n"); } nn_freemsg(buf); // 释放接收到的消息内存 } nn_close(sock); // 关闭socket return 0; } 以上是一个服务端代码,客户端发送内容“123”服务器回复“abc”,我想要新增当服务端接受到一段json信息为{"module":"1","from":"2","time":"","service":"get_dp_version","args":[]}时,回复客户端一个json为{"module":"2","from":"3","time":"","service":"response_dp_version","data":{"dp_version":"v0.1"}},使用libjansson,json部分单独放在函数里,不要都堆在主函数里
时间: 2024-03-02 20:54:33 浏览: 103
mysql的udf编程之非阻塞超时重传
好的,你需要在服务端代码中新增对{"module":"1","from":"2","time":"","service":"get_dp_version","args":[]}的处理,并回复{"module":"2","from":"3","time":"","service":"response_dp_version","data":{"dp_version":"v0.1"}}。
以下是修改后的服务端代码,已经包含了对json的处理:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <nanomsg/nn.h>
#include <nanomsg/reqrep.h>
#include <jansson.h>
#define SERVER_ADDRESS "tcp://127.0.0.1:5555" // 服务器地址
void handle_request(char *buf, int sock) {
json_error_t error;
json_t *root = json_loads(buf, 0, &error); // 解析json消息
if (!root) {
fprintf(stderr, "json_loads error: %s\n", error.text);
return;
}
const char *service = json_string_value(json_object_get(root, "service"));
if (!service) {
fprintf(stderr, "invalid request: missing service\n");
json_decref(root);
return;
}
if (strcmp(service, "get_dp_version") == 0) {
json_t *response = json_pack("{s:s, s:s, s:s, s:{s:s}}", "module", "2", "from", "3", "time", "", "data", "dp_version", "v0.1");
char *response_str = json_dumps(response, 0); // 将json消息转为字符串
int response_len = strlen(response_str) + 1;
int bytes = nn_send(sock, response_str, response_len, 0); // 发送回复消息
if (bytes < 0) {
fprintf(stderr, "nn_send error: %s\n", nn_strerror(nn_errno()));
}
json_decref(response);
free(response_str);
} else {
printf("invalid request: unknown service %s\n", service);
}
json_decref(root);
}
int main() {
int sock = nn_socket(AF_SP, NN_REP); // 创建一个REP类型的socket
if (sock < 0) {
fprintf(stderr, "nn_socket error: %s\n", nn_strerror(nn_errno()));
return -1;
}
if (nn_bind(sock, SERVER_ADDRESS) < 0) { // 绑定服务器地址
fprintf(stderr, "nn_bind error: %s\n", nn_strerror(nn_errno()));
nn_close(sock);
return -1;
}
printf("server started, waiting for client...\n");
while (1) {
char *buf = NULL;
int bytes = nn_recv(sock, &buf, NN_MSG, 0); // 接收客户端请求消息
if (bytes < 0) {
fprintf(stderr, "nn_recv error: %s\n", nn_strerror(nn_errno()));
nn_close(sock);
return -1;
}
printf("server received: %s\n", buf);
handle_request(buf, sock);
nn_freemsg(buf); // 释放接收到的消息内存
}
nn_close(sock); // 关闭socket
return 0;
}
```
注意:需要在编译时链接libjansson库,命令为 `gcc -o server server.c -ljansson -lnanomsg`。
阅读全文