日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

Linux socket本地进程间通信之UDP

發(fā)布時(shí)間:2025/5/22 linux 84 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux socket本地进程间通信之UDP 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

當(dāng)套接字用于本地通信時(shí),可以使用結(jié)構(gòu)體struct sockaddr_un描述一個(gè)本地地址。

1 struct sockaddr_un{ 2 unsigned short sun_family; /*協(xié)議類(lèi)型*/ 3 char sun_path[108]; /*套接字文件路徑*/ 4 };

在本地通信中,每個(gè)套接字文件代表一個(gè)本地地址。

UNIX域用戶數(shù)據(jù)報(bào)套接字服務(wù)器端流程如下:

(1)創(chuàng)建UNIX域數(shù)據(jù)報(bào)套接字;socket(AF_LOCAL, SOCK_DGRAM, 0)

(2)填充本地信息結(jié)構(gòu)體(服務(wù)器);struct?sockaddr_un

(3)綁定本地地址(服務(wù)器的地址信息);bind( )

(4)接收客戶端的數(shù)據(jù);recvfrom( )

(5)發(fā)送數(shù)據(jù)給客戶端;sendto( )

服務(wù)器端代碼如下:

server.c

1 #include<stdio.h> 2 #include<sys/types.h> 3 #include<unistd.h> 4 #include<sys/socket.h> 5 #include<arpa/inet.h> 6 #include<netinet/in.h> 7 #include<string.h> 8 #include<sys/un.h> 9 #include<stdlib.h> 10 11 #define N 64 12 13 int main(int argc, const char *argv[]) 14 { 15 int sockfd; 16 struct sockaddr_un serveraddr, clientaddr; 17 char buf[N]; 18 socklen_t len = sizeof(clientaddr); 19 20 sockfd = socket(AF_LOCAL, SOCK_DGRAM, 0); 21 if(sockfd < 0) 22 { 23 perror("fail to sockfd"); 24 return -1; 25 } 26 27 serveraddr.sun_family = AF_LOCAL; 28 strcpy(serveraddr.sun_path, "mysocket"); 29 30 if(bind(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0) 31 { 32 perror("fail to bind"); 33 return -1; 34 } 35 36 while(1) 37 { 38 if(recvfrom(sockfd, buf, N, 0, (struct sockaddr*)&clientaddr, &len) < 0) 39 { 40 perror("fail to recvfrom"); 41 return -1; 42 } 43 if(strncmp(buf, "quit", 4) == 0) 44 { 45 break; 46 } 47 buf[strlen(buf) - 1] = '\0'; 48 printf("buf:%s\n", buf); 49 strcat(buf, "++++----"); 50 if(sendto(sockfd, buf, N, 0, (struct sockaddr*)&clientaddr, sizeof(clientaddr)) < 0) 51 { 52 perror("fail to sendto"); 53 return -1; 54 } 55 } 56 close(sockfd); 57 return 0; 58 }

UNIX域用戶數(shù)據(jù)報(bào)套接字客戶端流程如下:

(1)創(chuàng)建UNIX域數(shù)據(jù)報(bào)套接字;socket(AF_LOCAL, SOCK_DGRAM, 0)

(2)填充本地信息結(jié)構(gòu)體(服務(wù)器端和客戶端);struct?sockaddr_un

(3)綁定本地地址(客戶端的地址信息);bind( )

(4)發(fā)送數(shù)據(jù)給服務(wù)器端;sendto( )

(5)接收服務(wù)器端的數(shù)據(jù);recvfrom( )

客戶端代碼如下:

client.c

1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<sys/types.h> 4 #include<unistd.h> 5 #include<sys/socket.h> 6 #include<arpa/inet.h> 7 #include<netinet/in.h> 8 #include<sys/un.h> 9 #include<string.h> 10 11 #define N 64 12 13 int main(int argc, const char *argv[]) 14 { 15 int sockfd; 16 char buf[N]; 17 struct sockaddr_un serveraddr, clientaddr; 18 19 sockfd = socket(AF_LOCAL, SOCK_DGRAM, 0); 20 if(sockfd < 0) 21 { 22 perror("fail to sockfd"); 23 return -1; 24 } 25 26 serveraddr.sun_family = AF_LOCAL; 27 strcpy(serveraddr.sun_path, "mysocket"); 28 29 clientaddr.sun_family = AF_LOCAL; 30 strcpy(clientaddr.sun_path, "socket"); 31 32 if(bind(sockfd, (struct sockaddr*)&clientaddr, sizeof(clientaddr)) < 0) 33 { 34 perror("fail to bind"); 35 return -1; 36 } 37 38 while(1) 39 { 40 printf("<client>"); 41 fgets(buf, N, stdin); 42 if(sendto(sockfd, buf, N, 0, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0) 43 { 44 perror("fail to sendto"); 45 return -1; 46 } 47 if(strncmp(buf, "quit", 4) == 0) 48 { 49 break; 50 } 51 if(recvfrom(sockfd, buf, N, 0, NULL, NULL) < 0) 52 { 53 perror("fail to recvfrom"); 54 return -1; 55 } 56 printf("buf:%s\n", buf); 57 } 58 close(sockfd); 59 60 return 0; 61 }

?

?客戶端運(yùn)行結(jié)果如下:

?

服務(wù)器端運(yùn)行結(jié)果如下:

轉(zhuǎn)載于:https://www.cnblogs.com/yangziwen0709/p/5024697.html

總結(jié)

以上是生活随笔為你收集整理的Linux socket本地进程间通信之UDP的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。