#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和解析json分别单独放在一个函数里。不要都堆在主函数里
时间: 2024-03-02 16:54:13 浏览: 205
好的,以下是修改后的代码:
```c
#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" // 服务器地址
json_t *create_response_json()
{
json_t *root = json_object();
json_object_set_new(root, "module", json_string("2"));
json_object_set_new(root, "from", json_string("3"));
json_object_set_new(root, "time", json_string(""));
json_object_set_new(root, "service", json_string("response_dp_version"));
json_t *data = json_object();
json_object_set_new(data, "dp_version", json_string("v0.1"));
json_object_set_new(root, "data", data);
return root;
}
int handle_request(const char *request_str, char **response_str)
{
int ret = -1;
json_error_t error;
json_t *request = json_loads(request_str, 0, &error);
if (!request) {
fprintf(stderr, "json_loads error: on line %d: %s\n", error.line, error.text);
goto error;
}
const char *module = json_string_value(json_object_get(request, "module"));
const char *from = json_string_value(json_object_get(request, "from"));
const char *service = json_string_value(json_object_get(request, "service"));
if (!module || !from || !service) {
fprintf(stderr, "invalid request\n");
goto error;
}
if (strcmp(service, "get_dp_version") == 0) {
json_t *response = create_response_json();
if (!response) {
fprintf(stderr, "create_response_json error\n");
goto error;
}
*response_str = json_dumps(response, 0);
if (!*response_str) {
fprintf(stderr, "json_dumps error\n");
goto error;
}
ret = 0;
} else {
fprintf(stderr, "invalid service: %s\n", service);
}
error:
if (request) {
json_decref(request);
}
return ret;
}
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);
char *response = NULL;
if (handle_request(buf, &response) == 0) {
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);
free(response);
return -1;
}
free(response);
}
nn_freemsg(buf); // 释放接收到的消息内存
}
nn_close(sock); // 关闭socket
return 0;
}
```
其中,`handle_request`函数用于处理请求消息,如果请求消息合法并且`service`字段为`get_dp_version`,则调用`create_response_json`函数创建回复消息,否则打印错误信息。`create_response_json`函数用于创建回复消息的JSON对象。在主函数中,接收到请求消息后调用`handle_request`函数获取回复消息,并发送回复消息。
阅读全文