Linux 使用nanomsg库进行进程间通信

文章目录

  • 1、下载安装
  • 2、使用
    • 2.1、服务端
    • 2.2、客户端
    • 2.3、编译

1、下载安装

github地址:https://github.com/nanomsg/nanomsg

解压包
1、tar xvf nanomsg.tar.gz

进入目录
2、cd nanomsg

创建编译目录build,并移动到build目录
3、mkdir build
cd build

编译安装
4、cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
cmake --build .
ctest .
sudo cmake --build . --target install
sudo ldconfig
make install

2、使用

nanomsg是一个消息通信组件,主要通过c语言进行编写的,使用宽松的MIT许可开源,小、轻、快,非常方便。

nanomsg中有很多模型,比如:
1、pull/push 单向管道推送模式
2、sub/pub 消息广播模式
3、pair 端对端双向通信模式
4、req/res模型,收发模式

我们主要使用的是pair模型进行进程间通信:

2.1、服务端

server:创建一个pair模式的socket,然后将socket设置为非阻塞,将这个套接字加入到nn_poll中进行事假监控,当有事件来时,调用对应的回调函数,进行数据收发。

#define IPC_ADDR "ipc:///tmp/test.ipc"
#include 
#include 
#include 

sock_client = nn_socket(AF_SP, NN_PAIR);
if (sock_client < 0) {
    printf("nn_socket fail %d",sock_client);
    return -1;
}
// 设置非阻塞模式
int send_timeout = 0;
int rc = nn_setsockopt(sock_client, NN_SOL_SOCKET, NN_SNDTIMEO, &send_timeout, sizeof(send_timeout));
if (rc < 0) {
    printf("nn_setsockopt fail error %s",nn_strerror(nn_errno()));
    goto err;
}

rc = nn_bind(sock_client, IPC_ADDR);
if (rc < 0) {
    printf("nn_connect fail error %s",nn_strerror(nn_errno()));
    goto err;
}

    // 监听读写事件
struct nn_pollfd poll_items[MAX_EVENTS];
poll_items[0].fd = sock_client;
poll_items[0].events = NN_POLLIN | NN_POLOUT;
poll_items[0].revents = 0;
while (1) 
{
    // 等待事件
    rc = nn_poll(poll_items, 1, -1);
    if (rc < 0) {
        printf("nn_poll error: %s", nn_strerror(nn_errno()));
        goto err;
    }

    // 处理事件
    if (poll_items[0].revents & NN_POLLIN) {
        handle_input(sock_client);
    }
    if (poll_items[0].revents & NN_POLLOUT) {
         handle_output(sock);
    }

    // 重置事件
    poll_items[0].revents = 0;
}
nn_close(sock_client);

可以使用json数据格式进行,通信,本项目就是使用json进行通信使用cjson库

// 当有数据来时,处理读回调函数,执行以下代码,将数据读取出来,然后通过cjson库进行解析
char buffer[1024] =  {0};
int bytes_received = nn_recv(sock_client, mgs_buf, size, 0);
if (bytes_received == 0) {
    LOG_ERROR(c,"not data");
    return 1;
}
else if (bytes_received < 0) {
    LOG_ERROR(c,"nn_recv fail %d",bytes_received);
    return -1;
}
return 0;


// 回复数据,通过cjson将包组好,然后进行回发
int bytes_sent = nn_send(sock_client, msg_send, strlen(msg_send), 0);

2.2、客户端

    int sock = nn_socket(AF_SP, NN_PAIR);
    if (sock < 0) {
        printf("Error creating socket\n");
        return -1;
    }

    //int bind_result = nn_bind(sock, "ipc:///tmp/test.ipc");
    int bind_result = nn_connect(sock, "ipc:///tmp/test.ipc");
    if (bind_result < 0) {
        printf("Error binding socket\n");
        nn_close(sock);
        return -1;
    }

    while(1)
    {
    	char *msg = "Hello from server";
        int bytes_sent = nn_send(sock, msg,strlen(msg), 0);
        if (bytes_sent < 0) {
            printf("Error sending message\n");
            nn_close(sock);
            return -1;
        }
        char buffer[1024];
        int bytes_received = nn_recv(sock, buffer, sizeof(buffer), 0);
        if (bytes_received < 0) {
            printf("Error receiving message\n");
            nn_close(sock);
            return -1;
        }
    }
    nn_close(sock);

2.3、编译

gcc -o ipc_req ipc_req.c  -lnanomsg

你可能感兴趣的:(Linux,系统编程,linux,网络,运维,进程间通信)