zoukankan      html  css  js  c++  java
  • c/c++ unix ipc

    c/c++ unix ipc 

    一个例子
    1. //c_unix.c
    2. #include <stdio.h>
    3. #include <sys/types.h>
    4. #include <sys/socket.h>
    5. #include <sys/un.h>
    6. #define UNIX_DOMAIN "/tmp/UNIX.domain"
    7. int main(void)
    8. {
    9.     int connect_fd;
    10.     int ret;
    11.     char snd_buf[1024];
    12.     int i;
    13.     static struct sockaddr_un srv_addr;
    14. //creat unix socket
    15.     connect_fd=socket(PF_UNIX,SOCK_STREAM,0);
    16.     if(connect_fd<0)
    17.     {
    18.         perror("cannot create communication socket");
    19.         return 1;
    20.     }
    21.     srv_addr.sun_family=AF_UNIX;
    22.     strcpy(srv_addr.sun_path,UNIX_DOMAIN);
    23. //connect server
    24.     ret=connect(connect_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));
    25.     if(ret==-1)
    26.     {
    27.         perror("cannot connect to the server");
    28.         close(connect_fd);
    29.         return 1;
    30.     }
    31.     memset(snd_buf,0,1024);
    32.     strcpy(snd_buf,"message from client");
    33. //send info server
    34.     for(i=0;i<4;i++)
    35.         write(connect_fd,snd_buf,sizeof(snd_buf));
    36.     close(connect_fd);
    37.     return 0;
    38. }
     
    1. //s_unix.c
    2. #include <stdio.h>
    3. #include <sys/types.h>
    4. #include <sys/socket.h>
    5. #include <sys/un.h>
    6. #define UNIX_DOMAIN "/tmp/UNIX.domain"
    7. int main(void)
    8. {
    9.     socklen_t clt_addr_len;
    10.     int listen_fd;
    11.     int com_fd;
    12.     int ret;
    13.     int i;
    14.     static char recv_buf[1024];
    15.     int len;
    16.     struct sockaddr_un clt_addr;
    17.     struct sockaddr_un srv_addr;
    18.     listen_fd=socket(PF_UNIX,SOCK_STREAM,0);
    19.     if(listen_fd<0)
    20.     {
    21.         perror("cannot create communication socket");
    22.         return 1;
    23.     }
    24.     
    25.     //set server addr_param
    26.     srv_addr.sun_family=AF_UNIX;
    27.     strncpy(srv_addr.sun_path,UNIX_DOMAIN,sizeof(srv_addr.sun_path)-1);
    28.     unlink(UNIX_DOMAIN);
    29.     //bind sockfd & addr
    30.     ret=bind(listen_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));
    31.     if(ret==-1)
    32.     {
    33.         perror("cannot bind server socket");
    34.         close(listen_fd);
    35.         unlink(UNIX_DOMAIN);
    36.         return 1;
    37.     }
    38.     //listen sockfd
    39.     ret=listen(listen_fd,1);
    40.     if(ret==-1)
    41.     {
    42.         perror("cannot listen the client connect request");
    43.         close(listen_fd);
    44.         unlink(UNIX_DOMAIN);
    45.         return 1;
    46.     }
    47.     //have connect request use accept
    48.     len=sizeof(clt_addr);
    49.     com_fd=accept(listen_fd,(struct sockaddr*)&clt_addr,&len);
    50.     if(com_fd<0)
    51.     {
    52.         perror("cannot accept client connect request");
    53.         close(listen_fd);
    54.         unlink(UNIX_DOMAIN);
    55.         return 1;
    56.     }
    57.     //read and printf sent client info
    58.     printf("/n=====info=====/n");
    59.     for(i=0;i<4;i++)
    60.     {
    61.         memset(recv_buf,0,1024);
    62.         int num=read(com_fd,recv_buf,sizeof(recv_buf));
    63.         printf("Message from client (%d)) :%s/n",num,recv_buf);
    64.     }
    65.     close(com_fd);
    66.     close(listen_fd);
    67.     unlink(UNIX_DOMAIN);
    68.     return 0;
    69. }
  • 相关阅读:
    CentOS 设置mysql的远程访问
    centos的防火墙命令
    gorm的related理解和实例
    epoll相比select,poll的2个改进点
    limit越往后越慢,如何解决?
    LRUCache的设计,实现和调试
    map可以并发读,不能并发写
    2020年4月上旬算法讨论4(快排和堆排)
    删除链表节点代码编写复盘(从直接思路到优雅思路)
    2020年3月下寻算法讨论3(链表-下)
  • 原文地址:https://www.cnblogs.com/timssd/p/4781101.html
Copyright © 2011-2022 走看看