跨进程通讯之Unix Socket通讯

文章详细描述了两个C语言程序,unix_client.c用于建立客户端连接到Unix域套接字服务器,而unix_socket.c则实现了一个简单的服务器,监听并处理客户端请求。编译和运行这两个程序以展示客户端和服务端的交互过程。

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)) &l

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值