Linux socket本地进程间通信之UDP
生活随笔
收集整理的這篇文章主要介紹了
Linux socket本地进程间通信之UDP
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
當套接字用于本地通信時,可以使用結構體struct sockaddr_un描述一個本地地址。
1 struct sockaddr_un{ 2 unsigned short sun_family; /*協議類型*/ 3 char sun_path[108]; /*套接字文件路徑*/ 4 };在本地通信中,每個套接字文件代表一個本地地址。
UNIX域用戶數據報套接字服務器端流程如下:
(1)創建UNIX域數據報套接字;socket(AF_LOCAL, SOCK_DGRAM, 0)
(2)填充本地信息結構體(服務器);struct?sockaddr_un
(3)綁定本地地址(服務器的地址信息);bind( )
(4)接收客戶端的數據;recvfrom( )
(5)發送數據給客戶端;sendto( )
服務器端代碼如下:
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域用戶數據報套接字客戶端流程如下:
(1)創建UNIX域數據報套接字;socket(AF_LOCAL, SOCK_DGRAM, 0)
(2)填充本地信息結構體(服務器端和客戶端);struct?sockaddr_un
(3)綁定本地地址(客戶端的地址信息);bind( )
(4)發送數據給服務器端;sendto( )
(5)接收服務器端的數據;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 }?
?客戶端運行結果如下:
?
服務器端運行結果如下:
轉載于:https://www.cnblogs.com/yangziwen0709/p/5024697.html
總結
以上是生活随笔為你收集整理的Linux socket本地进程间通信之UDP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql事务和锁InnoDB
- 下一篇: php -- 检查是否存在