乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      unix domain socket示例一(SOCK

       just_person 2019-06-24

         unix domain socket 是IPC通信的一種方式,可用于與管理進(jìn)程間通信,同時(shí)由和網(wǎng)絡(luò)socket統(tǒng)一,所以很好管理,使用還是比較多。現(xiàn)舉個(gè)例子:

         server.c

          

      復(fù)制代碼
       1 #include <stdio.h>
       2 #include <string.h>
       3 #include <unistd.h>
       4 #include <stdlib.h>
       5 #include <sys/types.h>
       6 #include <sys/socket.h>
       7 #include <sys/un.h>
       8 #include <stddef.h>
       9 
      10 #define SRC_ADDR "/var/run/uds_test.socket"
      11 
      12 int main (int argc, char **argv)
      13 {
      14     int sockfd;
      15     struct sockaddr_un src;
      16     int ret;
      17 
      18     unlink (SRC_ADDR);
      19     sockfd = socket (AF_UNIX, SOCK_DGRAM, 0);
      20     if (sockfd < 0) {
      21         perror ("create socket failed");
      22         exit (EXIT_FAILURE);
      23     }
      24 
      25     memset (&src, 0, sizeof (src));
      26     src.sun_family = AF_UNIX;
      27     strcpy (src.sun_path, SRC_ADDR);
      28     int len;
      29     len = offsetof (struct sockaddr_un, sun_path) +
      30         sizeof (SRC_ADDR);
      31 
      32     if (bind (sockfd, (struct sockaddr *)&src, len) < 0) {
      33         perror ("bind socket failed");
      34         exit (EXIT_FAILURE);
      35     }
      36 
      37     size_t size = 0;
      38     char buf[BUFSIZ] ={'\0'};
      39     for (;;) {
      40         size = recvfrom (sockfd,buf, BUFSIZ,0,
      41                 NULL, NULL);
      42 
      43         if (size > 0)
      44             printf ("recv: %s\n", buf);
      45 
      46     }
      47 
      48     return 0;
      49 }
      復(fù)制代碼

        client.c

      復(fù)制代碼
      #include <stdio.h>
      #include <string.h>
      #include <unistd.h>
      #include <stdlib.h>
      #include <sys/types.h>
      #include <sys/socket.h>
      #include <sys/un.h>
      #include <stddef.h>
      #include <time.h>
      
      #define DST_ADDR "/var/run/uds_test.socket"
      
      int main (int argc, char **argv)
      {
          int sockfd;
          struct sockaddr_un dst;
          int ret;
      
          sockfd = socket (AF_UNIX, SOCK_DGRAM, 0);
          if (sockfd < 0) {
              perror ("create socket failed");
              exit (EXIT_FAILURE);
          }
      
          memset (&dst, 0, sizeof (dst));
          dst.sun_family = AF_UNIX;
          strcpy (dst.sun_path, DST_ADDR);
          int len;
          len = offsetof (struct sockaddr_un, sun_path) +
              sizeof (DST_ADDR);
      
          time_t t;
          char *str;
      
          for (;;) {
              t = time (NULL);
              str = ctime (&t);
              if (str == NULL)
                  break;
      
              sendto (sockfd, str, strlen (str),0,
                      (struct sockaddr *)&dst,len);
      
              sleep (1);
          }
      
          return 0;
      
          
      }
      復(fù)制代碼

      這個(gè)demo實(shí)現(xiàn)client端讀取當(dāng)前的時(shí)間,然后通過UDS發(fā)送給服務(wù)器端,結(jié)果如圖所示:

      一個(gè)簡單的示例,希望能給你提供些思路

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多