yyyyyyyyyyyyyyyyyyyy

博客园 首页 新随笔 联系 订阅 管理

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. }
posted on 2015-09-04 01:38  xxxxxxxx1x2xxxxxxx  阅读(206)  评论(0编辑  收藏  举报