Linux 域通信

服务器端

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/un.h>
#include <errno.h> // 包含 errno.h 头文件
int main() {
    struct sockaddr_un myaddr;
    int listenfd, connfd;
    char buf[200];
    ssize_t len;

    // 创建Unix域套接字
    listenfd = socket(PF_UNIX, SOCK_STREAM, 0);
    if (listenfd < 0) {
        perror("socket");
        exit(1);
    }

    // 绑定套接字到地址
    bzero(&myaddr, sizeof(myaddr));
    myaddr.sun_family = AF_UNIX;
    strcpy(myaddr.sun_path, "vivo50");
    if (unlink(myaddr.sun_path) < 0 && errno != ENOENT) {
        perror("unlink");
        close(listenfd);
        exit(1);
    }
    if (bind(listenfd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
        perror("bind");
        close(listenfd);
        exit(1);
    }

    // 监听连接
    if (listen(listenfd, 5) < 0) {
        perror("listen");
        close(listenfd);
        exit(1);
    }

    // 接受连接
    printf("等待连接...\n");
    connfd = accept(listenfd, NULL, NULL);
    if (connfd < 0) {
        perror("accept");
        close(listenfd);
        exit(1);
    }

    // 通信循环
    while (1) {
        bzero(buf, sizeof(buf));
        len = read(connfd, buf, sizeof(buf) - 1); // 读取数据
        if (len <= 0) {
            printf("客户端断开连接。\n");
            break;
        }
        buf[len] = '\0'; // 确保字符串以NULL结尾
        printf("客户端: %s\n", buf);

        if (strncmp(buf, "quit", strlen("quit")) == 0) {
            break;
        }

        // 回显客户端发送的数据
        write(connfd, buf, len); // 发送数据
    }

    // 关闭套接字
    close(connfd);
    close(listenfd);
    unlink(myaddr.sun_path); // 删除套接字文件
    return 0;
}

客户端

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/un.h>
#include <errno.h> // 包含 errno.h 头文件
 #include <pthread.h>

void *RcvMsg(void *arg) {
    int sockfd = *(int *)arg;
    char buf[200];

    while (1) {
        bzero(buf, sizeof(buf));
        ssize_t len = recv(sockfd, buf, sizeof(buf) - 1, 0); // 接收消息
        if (len <= 0) {
            printf("服务器断开连接。\n");
            close(sockfd);
            break;
        }
        buf[len] = '\0'; // 确保字符串以NULL结尾
        printf("服务器: %s\n", buf);

        if (strncmp(buf, "quit", strlen("quit")) == 0) {
            break;
        }
    }
    return NULL;
}

int main() {
    struct sockaddr_un myaddr;
    char buf[200];
    int sockfd;

    // 创建Unix域套接字
    sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
    if (sockfd < 0) {
        perror("socket");
        exit(1);
    }

    // 连接到服务器
    bzero(&myaddr, sizeof(myaddr));
    myaddr.sun_family = PF_UNIX;
    strcpy(myaddr.sun_path, "vivo50");
    if (connect(sockfd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
        perror("connect");
        close(sockfd);
        exit(1);
    }

    // 创建接收消息的线程
    pthread_t thread1;
    if (pthread_create(&thread1, NULL, RcvMsg, &sockfd) != 0) {
        perror("pthread_create");
        close(sockfd);
        exit(1);
    }
    if (pthread_detach(thread1) != 0) {
        perror("pthread_detach");
        close(sockfd);
        exit(1);
    }

    // 与服务器通信
    while (1) {
        bzero(buf, sizeof(buf));
        printf("请输入数据: ");
        if (fgets(buf, sizeof(buf), stdin) == NULL) {
            break;
        }
        send(sockfd, buf, strlen(buf), 0);

        if (strncmp(buf, "quit", strlen("quit")) == 0) {
            break;
        }
    }

    // 断开连接
    close(sockfd);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前辈等等我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值