跨进程通讯之Unix Socket通讯

1、unix_client.c代码

#include <stdlib.h>

#include <stdio.h>

#include <stddef.h>

#include <sys/socket.h>

#include <sys/un.h>

#include <errno.h>

#include <string.h>

#include <unistd.h>

#define MAXLINE 80

char *client_path = "client-socket";

char *server_path = "server-socket";

int main() {

        struct  sockaddr_un cliun, serun;

        int len;

        char buf[100];

        int sockfd, n;

        if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){

                perror("client socket error");

                exit(1);

        }

    memset(&serun, 0, sizeof(serun));

    serun.sun_family = AF_UNIX;

    strncpy(serun.sun_path,server_path ,

                   sizeof(serun.sun_path) - 1);

    if (connect(sockfd, (struct sockaddr *)&serun, sizeof(struct sockaddr_un)) < 0){

        perror("connect error");

        exit(1);

    }

    printf("please input send char:");

    while(fgets(buf, MAXLINE, stdin) != NULL) {

         write(sockfd, buf, strlen(buf));

         n = read(sockfd, buf, MAXLINE);//读不到了就为0

         if ( n <= 0 ) {

            printf("the other side has been closed.\n");

            break;

         }else {

            printf("received from server: %s \n",buf);

         }

         printf("please input send char:");

    }

    close(sockfd);

    return 0;

}

2、unix_socket.c代码

#include <stdlib.h>

#include <stdio.h>

#include <stddef.h>

#include <sys/socket.h>

#include <sys/un.h>

#include <errno.h>

#include <string.h>

#include <unistd.h>

#include <ctype.h>

#define MAXLINE 80

char *socket_path = "server-socket";

int main()

{

    struct sockaddr_un serun, cliun;

    socklen_t cliun_len;

    int listenfd, connfd, size;

    char buf[MAXLINE];

    int i, n;

    if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {

        perror("socket error");

        exit(1);

    }

    memset(&serun, 0, sizeof(serun));

    serun.sun_family = AF_UNIX;

    strncpy(serun.sun_path,socket_path ,

                   sizeof(serun.sun_path) - 1);

    unlink(socket_path);//这个相当于把之前的地址要移除,不然上一个server没有结束,移除会报错already in use

    if (bind(listenfd, (struct sockaddr *)&serun, sizeof(struct sockaddr_un)) < 0) {

        perror("bind error");

        exit(1);

    }

    printf("UNIX domain socket bound\n");

    if (listen(listenfd, 20) < 0) {

        perror("listen error");

        exit(1);

    }

    printf("Accepting connections ...\n");

    while(1) {

        cliun_len = sizeof(cliun);

        if ((connfd = accept(listenfd, (struct sockaddr *)&cliun, &cliun_len)) < 0){

            perror("accept error");

            continue;

        }

        printf("new client connect to server,client sockaddr === %s \n",((struct sockaddr *)&cliun)->sa_data);

        while(1) {

            memset(buf,0,sizeof(buf));

            n = read(connfd, buf, sizeof(buf));

            if (n < 0) {

                perror("read error");

                break;

            } else if(n == 0) {

                printf("EOF\n");

                break;

            }

            printf("received: %s\n", buf);

            

            if(strncmp(buf,"quit",4)==0){

                break;

            }

            for(i = 0; i < n; i++) {

                buf[i] = toupper(buf[i]);

            }

            write(connfd, buf, n);

        }

        close(connfd);

    }

    close(listenfd);

    return 0;

}

3、命令

编译:

gcc unix_client.c -o unix_client

gcc unix_socket.c -o unix_socket

运行:

./unix_socket  //先运行服务端,服务端运行起来,客户端才能连接

./unix_client   //再运行客户端

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unix Socket 是一种进程间通信(IPC)的机制,它允许在同一台机器上的进程之间进行通信。它与网络 socket 类似,但是不依赖于网络协议栈,而是直接在操作系统内核中实现。 Unix Socket 提供了一种可靠的、高性能的进程间通信方式,适用于同一台机器上的进程之间的通信。它可以用于多种编程语言,并且在 Unix-like 操作系统中广泛使用。 使用 Unix Socket 进行通信的过程包括以下几个步骤: 1. 创建 Socket:使用 socket() 系统调用创建一个新的 socket,并指定通信域(如 AF_UNIX)、类型(如 SOCK_STREAM 或 SOCK_DGRAM)和协议(如 0)。 2. 绑定地址:使用 bind() 系统调用将 socket 绑定到一个特定的文件路径或抽象命名空间。 3. 监听连接(可选):如果使用的是面向流的 socket(如 SOCK_STREAM),则可以使用 listen() 系统调用来监听连接请求。 4. 接受连接(可选):如果使用的是面向流的 socket(如 SOCK_STREAM),则可以使用 accept() 系统调用接受新的连接请求,并创建一个新的 socket 用于与客户端进行通信。 5. 发送和接收数据:使用 send() 和 recv() 等系统调用在 socket 之间进行数据的发送和接收。 6. 关闭连接:使用 close() 系统调用关闭 socket 连接。 通过这些步骤,进程可以通过 Unix Socket 在同一台机器上进行可靠的双向通信。它的优点包括低延迟、高性能和简单的 API 接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值