跨进程通讯之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 //再运行客户端

相关推荐
千里马学框架5 小时前
重学安卓14/15自由窗口freeform企业实战bug-学员作业
android·framework·bug·systrace·安卓framework开发·安卓窗口系统·自由窗口
农民小飞侠9 小时前
ubuntu 24.04 error: cannot uninstall blinker 1.7.0, record file not found. hint
linux·运维·ubuntu
进取星辰11 小时前
24、TypeScript:预言家之书——React 19 类型系统
linux·运维·ubuntu
lifei_000112 小时前
VM中 ubuntu 网卡不显示
运维·服务器·ubuntu
码码哈哈爱分享12 小时前
【2025最新】Vm虚拟机中直接使用Ubuntu 免安装过程直接使用教程与下载
linux·运维·ubuntu
星寂樱易李17 小时前
Ubuntu 18.04 iso文件下载
linux·运维·ubuntu
风行無痕1 天前
Ubuntu Linux系统配置账号无密码sudo
linux·服务器·ubuntu
o0o_-_1 天前
【瞎折腾/mi50 32G/ubuntu】mi50显卡ubuntu运行大模型开坑(三)安装风扇并且控制转速
linux·运维·ubuntu
Huazzi.1 天前
Ubuntu 22虚拟机【网络故障】快速解决指南
linux·网络·学习·ubuntu·bash·编程
门前云梦1 天前
VirtualBox中安装并运行ubuntu-24.04.2-desktop虚拟机
linux·ubuntu·虚拟机·virtualbox