UNIX domain ( UNIX 预协议 ) 实例


一般在我们自己的主机上通信的时候,前面也讲到过可以使用pipe,fifo,msg,共享内存之类,如果你想使用套接字的话,当然TCP/IP的套接字也是可以的,只要指定server的IP = 127.0.0.1 或者你的当前主机的实际接入网络的IP也是可以的!但是相对与此处的UNIX domain来说,在效率上可能会低一点点、、、

UNIX domain的实际操作和前面的TCP/IP中的实际的操作的框架流程是差不多的!只是其中的细节的变化要注意!

代码贴下:

server端:

  1. /* 
  2.     在主机之间的通信可以使用TCP/IP套接字进行通信, 
  3.     但是在实现的效率上是不如UNIX DOMAIN的,所以 
  4.     此处看看这个协议的处理! 
  5.      
  6.     其实实际操作中我们可以知道,与TCP/IP的操作流程 
  7.     是差不多的!只是在一些细节上的变化! 
  8. */  
  9.   
  10. #include <stdio.h>   
  11. #include <stdlib.h>   
  12. #include <errno.h>   
  13. #include <string.h>   
  14. #include <sys/types.h>   
  15. #include <sys/socket.h>   
  16. #include <sys/un.h>   
  17.   
  18. #define     FILE            "UNIX_DOMAIN"       //!> 有点像FIFO中的文件是吧,呵呵   
  19. #define     MAXSIZE     1024   
  20. #define     MAXBACK 100   
  21.   
  22. int main( int argc, char ** argv )  
  23. {  
  24.     int         listen_fd;  
  25.     int         conn_fd;  
  26.     int         len;  
  27.     char    recv[MAXSIZE];  
  28.     struct sockaddr_un  servaddr;  
  29.     struct sockaddr_un  childaddr;  
  30.   
  31.     unlink( FILE );         //!> 保证么有已经存在的文件   
  32.   
  33.     //!> establish the socket   
  34.         //!>    
  35.     if( ( listen_fd  = socket( AF_UNIX, SOCK_STREAM, 0 ) ) == -1 )  
  36.     {  
  37.         printf("Socket error : %d\n", errno);  
  38.         exit( EXIT_FAILURE );  
  39.     }  
  40.       
  41.     servaddr.sun_family=AF_UNIX;        //!> UNIX DOMAIN   
  42.         strncpy( servaddr.sun_path, FILEsizeof( servaddr.sun_path ) - 1 );  //!> FILE   
  43.       
  44.         //!> 绑定   
  45.         //!>   
  46.     if( bind( listen_fd, ( struct sockaddr * )&servaddr, sizeof( servaddr ) ) == -1 )  
  47.     {  
  48.         printf("Bind Error : %d\n", errno);  
  49.         close( listen_fd );  
  50.         unlink( FILE );  
  51.         exit( EXIT_FAILURE );  
  52.     }  
  53.       
  54.     //!> 监听   
  55.     //!>   
  56.     if( listen( listen_fd, MAXBACK ) == -1 )  
  57.     {  
  58.         printf("Listen error : %d\n", errno);  
  59.         close( listen_fd );  
  60.         unlink( FILE );  
  61.         exit( EXIT_FAILURE );  
  62.     }  
  63.       
  64.     len = sizeof( childaddr );  
  65.   
  66.     while( 1 )  
  67.     {  
  68.         //!> accept : 你懂得~   
  69.         if( ( conn_fd = accept( listen_fd, ( struct sockaddr *)&childaddr, &len ) ) == -1 )  
  70.         {  
  71.             printf("Accept Error : %d\n", errno);  
  72.             close( listen_fd );  
  73.             unlink( FILE );  
  74.             exit( EXIT_FAILURE );  
  75.         }  
  76.           
  77.         while( 1 )              //!> 可能连续读一个套接口上的内容!   
  78.         {  
  79.             memset( recv, 0, sizeof( recv ) );        
  80.             len = read( conn_fd, recv, sizeof( recv ) );  
  81.   
  82.             if( len == 0 )  
  83.             {  
  84.                 close( conn_fd );  
  85.                 break;  
  86.             }  
  87.             else if( len < 0 )  
  88.             {  
  89.                 printf("Read error...  : %d\n", errno);  
  90.                 unlink( FILE );  
  91.                 close( conn_fd );  
  92.                 close( listen_fd );  
  93.                 exit( EXIT_FAILURE );  
  94.             }  
  95.             else  
  96.             {  
  97.                 len = strlen( recv );  
  98.                 recv[len] = '\0';  
  99.                   
  100.                 write( conn_fd, recv, strlen( recv ) );  
  101.             }  
  102.         }  
  103.               
  104.     }  
  105.   
  106.     unlink( FILE );  
  107.     close( conn_fd );  
  108.     close( listen_fd );  
  109.   
  110.     return 0;  
  111. }  
  • 1
  • 2
  • 下一页

相关内容