zoukankan      html  css  js  c++  java
  • 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] = '';
    48         printf("buf:%s
    ", 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
    ", buf);
    57     }
    58     close(sockfd);
    59 
    60     return 0;
    61 }

     客户端运行结果如下:

    服务器端运行结果如下:

  • 相关阅读:
    使用node.js搭建一个简单的后台服务
    node.js连接MySQL数据库
    js将date对象转换为指定格式
    配置angular2运行环境
    简单AJAX请求JSon数据
    正则表达式
    javascript typeof 和 instanceof 的区别和联系
    map和reduce函数的使用
    Github 上管理项目
    微服务资料
  • 原文地址:https://www.cnblogs.com/yangziwen0709/p/5024697.html
Copyright © 2011-2022 走看看