手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux 下服务器设计

[日期:2012-04-21] 来源:Linux社区  作者:shanshanpt [字体:大 中 小]
上一篇(http://www.linuxidc.com/Linux/2012-04/59007.htm)的介绍已经OK,那么此处就只贴上代码!

   CODE_1 : 主线程accept,但是线程比进程有一个好处是,对于conn_fd资源来说是可以直接共享的,所以就可以不需要像在进程中那样需要传递socket,所以设计会简单一点!但是同样注意互斥的问题!

   Server:

  
  1. /* 
  2.     主线程预先创建一批线程,线程进行等待,然后主线程 
  3.     accept, 子线程互斥抢占conn_fd,然后处理数据 
  4. */  
  5.   
  6. #include <stdio.h>   
  7. #include <unistd.h>   
  8. #include <stdlib.h>   
  9. #include <string.h>   
  10. #include <pthread.h>   
  11. #include <sys/select.h>   
  12. #include <sys/types.h>   
  13. #include <errno.h>   
  14. #include <sys/socket.h>   
  15. #include <netinet/in.h>   
  16. #include <fcntl.h>   
  17.   
  18. #define PORT            6000   
  19. #define MAXBACK 100   
  20. #define MAXLINE     1024   
  21. #define CHILD_NUM   10   
  22.   
  23. pthread_mutex_t     g_mutex;        //!> 互斥量   
  24. pthread_cond_t      g_cond;     //!> 条件变量   
  25.   
  26. int         connfds[MAXBACK];   //!> 保存连接s   
  27. int         i_get = 0;      //!> 取出connfd   
  28. int         i_put = 0;      //!> 放进去connfd   
  29.   
  30. void * thread_do( void * arg )  
  31. {  
  32.     int         i = 0;  
  33.     int     n_read;  
  34.     int     conn_fd;  
  35.     char    buf[MAXLINE];  
  36.       
  37.     while( 1 )  
  38.     {  
  39.         pthread_mutex_lock( &g_mutex );         //!> 加锁   
  40.           
  41.         while( i_get == i_put )             //!> 当前缓冲区没有connfd   
  42.         {                           //!> 阻塞   
  43.             pthread_cond_wait( &g_cond, &g_mutex );  
  44.         }  
  45.           
  46.         conn_fd = connfds[i_get];           //!> 获取conn_fd   
  47.           
  48.         if( ++i_get == MAXBACK )            //!> 到头   
  49.         {  
  50.             i_get = 0;  
  51.         }  
  52.           
  53.         pthread_mutex_unlock( &g_mutex );       //!> 解锁   
  54.           
  55.         while( strcmp( buf, "Q" ) != 0 && strcmp( buf, "q" ) != 0  )  
  56.         {  
  57.           
  58.             if( ( n_read = read( conn_fd, buf, MAXLINE ) ) < 0 )  
  59.             {  
  60.                 printf( "Read errnr! :%d \n", errno );  
  61.                 exit( EXIT_FAILURE );  
  62.             }  
  63.             else if( n_read == 0 )  
  64.             {  
  65.                 continue;  
  66.             }  
  67.             else  
  68.             {  
  69.                 while( buf[i] )  
  70.                 {  
  71.                     buf[i] = toupper( buf[i] );  
  72.                     i++;  
  73.                 }  
  74.               
  75.                 printf("Child %d done! \n", ( int )pthread_self());  
  76.                 printf("Child %d send %s\n", ( int )pthread_self(), buf);  
  77.                 write( conn_fd, buf, strlen( buf ) );           //!> 写回给client   
  78.             }  
  79.         }  
  80.     }  
  81. }  
  82.   
  83. //!> MAIN    
  84. //!>    
  85. int main( int argc, char ** argv )  
  86. {  
  87.     pthread_t       tids[CHILD_NUM];  
  88.     int     i;  
  89.     int         listen_fd;  
  90.     int         conn_fd;  
  91.     int     n_read;  
  92.     char    buf[5];  
  93.     struct sockaddr_in servaddr;  
  94.     struct sockaddr_in cliaddr;  
  95.     int     len = sizeof( struct sockaddr_in );  
  96.       
  97.     //!>下面需要设置互斥   
  98.     //!>    
  99.     pthread_mutex_init( &g_mutex, NULL );   //!> 互斥量初始化   
  100.     pthread_cond_init( &g_cond, NULL ); //!> 条件变量初始化   
  101.       
  102.     //!> server 套接口   
  103.     //!>    
  104.     bzero( &servaddr, sizeof( servaddr ) );  
  105.     servaddr.sin_family = AF_INET;  
  106.     servaddr.sin_addr.s_addr = htonl( INADDR_ANY );  
  107.     servaddr.sin_port = htons( PORT );  
  108.       
  109.     //!> 建立套接字   
  110.     //!>    
  111.     if( ( listen_fd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )  
  112.     {  
  113.         printf("Socket Error...\n" , errno );  
  114.         exit( EXIT_FAILURE );  
  115.     }  
  116.       
  117.     //!> 绑定   
  118.     //!>   
  119.     if( bind( listen_fd, ( struct sockaddr *)&servaddr, sizeof( servaddr ) ) == -1 )  
  120.     {  
  121.         printf("Bind Error : %d\n", errno);  
  122.         exit( EXIT_FAILURE );  
  123.     }  
  124.       
  125.     //!> 监听   
  126.     //!>    
  127.     if( listen( listen_fd, MAXBACK ) == -1 )  
  128.     {  
  129.         printf("Listen Error : %d\n", errno);  
  130.         exit( EXIT_FAILURE );  
  131.     }  
  132.       
  133.     for( i = 0; i < CHILD_NUM; i++ )     //!> 创建10个线程   
  134.     {  
  135.         if( pthread_create( &tids[i], NULL, thread_do, NULL ) != 0 )  
  136.         {  
  137.             printf("Create %d thread error! : %d", i, errno);  
  138.             exit( EXIT_FAILURE );  
  139.         }  
  140.     }  
  141.       
  142.     while( 1 )  
  143.     {  
  144.         if( ( conn_fd = accept( listen_fd, ( struct sockaddr *)&cliaddr , &len ) ) == -1 )  
  145.         {  
  146.             printf("Accept error! :%d\n", errno);  
  147.             exit( EXIT_FAILURE );  
  148.         }  
  149.           
  150.         pthread_mutex_lock( &g_mutex );     //!> 加锁   
  151.           
  152.         connfds[i_put] = conn_fd;       //!> 放进缓存区   
  153.           
  154.         if( ++i_put == MAXBACK )        //!> 到头   
  155.         {  
  156.             i_put = 0;  
  157.         }  
  158.           
  159.         if( i_get == i_put )  
  160.         {  
  161.             printf( "Get and put error! :%d\n", errno );  
  162.             exit( EXIT_FAILURE );  
  163.         }  
  164.           
  165.         pthread_cond_signal( &g_cond );     //!> 给一个信号说明有数据了   
  166.         pthread_mutex_unlock( &g_mutex );   //!> 解锁   
  167.           
  168.     }  
  169.       
  170.     pthread_mutex_destroy( &g_mutex );      //!> 删除互斥灯   
  171.     pthread_cond_destroy( &g_cond );  
  172.       
  173.     return 0;  
  174. }  

Client 和进程中的一样,可以直接copy!

CODE_2 : 直接由子线程accept,还是互斥问题( 这个问题比较简单 )

  1. /* 
  2.     主线成创建n个子线程,然后主线成就没有事做了, 
  3.     进入pause,由子线程互斥调用accept处理 
  4. */  
  5.   
  6. #include <stdio.h>   
  7. #include <unistd.h>   
  8. #include <stdlib.h>   
  9. #include <string.h>   
  10. #include <pthread.h>   
  11. #include <sys/select.h>   
  12. #include <sys/types.h>   
  13. #include <errno.h>   
  14. #include <sys/socket.h>   
  15. #include <netinet/in.h>   
  16. #include <fcntl.h>   
  17.   
  18. #define PORT        6000   
  19. #define MAXBACK 100   
  20. #define MAXLINE     1024   
  21. #define CHILD_NUM   10   
  22.   
  23. pthread_mutex_t     g_mutex;        //!> 互斥量   
  24.   
  25. //!> 子线程子线程执行函数   
  26. //!>    
  27. void * thread_do( void * arg )  
  28. {  
  29.     struct sockaddr_in cliaddr;  
  30.     int     len = sizeof( struct sockaddr_in );  
  31.     int     conn_fd;  
  32.     int     n_read;  
  33.     int     i;  
  34.     char    buf[MAXLINE];  
  35.       
  36.     int     *listen_fd = (int *)arg;//!> 监听口号       
  37.     while( 1 )  
  38.     {  
  39.         pthread_mutex_lock( &g_mutex ); //!> 加锁   
  40.           
  41.         if( ( conn_fd = accept( *listen_fd, ( struct sockaddr *)&cliaddr , &len ) ) == -1 )  
  42.         {  
  43.             printf(" Thread %d accept error! :%d \n", ( int )pthread_self(), errno);  
  44.             exit( EXIT_FAILURE );  
  45.         }  
  46.           
  47.         pthread_mutex_unlock( &g_mutex );//!> 加锁   
  48.       
  49.         while( strcmp( buf, "Q" ) != 0 && strcmp( buf, "q" ) != 0  )  
  50.         {  
  51.             memset( buf, 0, sizeof( buf ) );  
  52.               
  53.             if( ( n_read = read( conn_fd, buf, MAXLINE ) ) < 0 )  
  54.             {  
  55.                 printf( "Read errnr! :%d \n", errno );  
  56.                 exit( EXIT_FAILURE );  
  57.             }  
  58.             else if( n_read == 0 )  
  59.             {  
  60.                 continue;  
  61.             }  
  62.             else  
  63.             {  
  64.                 while( buf[i] )  
  65.                 {  
  66.                     buf[i] = toupper( buf[i] );  
  67.                     i++;  
  68.                 }  
  69.               
  70.                 printf("Child %d done! \n", ( int )pthread_self());  
  71.                 printf("Child %d send %s\n", ( int )pthread_self(), buf);  
  72.                 write( conn_fd, buf, strlen( buf ) );           //!> 写回给client   
  73.             }  
  74.         }  
  75.     }  
  76.       
  77. }  
  78.   
  79. //!> MAIN    
  80. //!>   
  81. int main( int argc, char ** argv )  
  82. {  
  83.     int     i;  
  84.     int         listen_fd;  
  85.     int         conn_fd;  
  86.     int     n_read;  
  87.     char            buf[5];  
  88.     pthread_t   tids[CHILD_NUM];  
  89.     struct sockaddr_in servaddr;  
  90.   
  91.     //!>  互斥锁   
  92.     //!>    
  93.     pthread_mutex_init( &g_mutex, NULL );   //!> 互斥量初始化   
  94.       
  95.     //!> server 套接口   
  96.     //!>    
  97.     bzero( &servaddr, sizeof( servaddr ) );  
  98.     servaddr.sin_family = AF_INET;  
  99.     servaddr.sin_addr.s_addr = htonl( INADDR_ANY );  
  100.     servaddr.sin_port = htons( PORT );  
  101.       
  102.     //!> 建立套接字   
  103.     //!>    
  104.     if( ( listen_fd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )  
  105.     {  
  106.         printf("Socket Error...\n" , errno );  
  107.         exit( EXIT_FAILURE );  
  108.     }  
  109.       
  110.     //!> 绑定   
  111.     //!>   
  112.     if( bind( listen_fd, ( struct sockaddr *)&servaddr, sizeof( servaddr ) ) == -1 )  
  113.     {  
  114.         printf("Bind Error : %d\n", errno);  
  115.         exit( EXIT_FAILURE );  
  116.     }  
  117.       
  118.     //!> 监听   
  119.     //!>    
  120.     if( listen( listen_fd, MAXBACK ) == -1 )  
  121.     {  
  122.         printf("Listen Error : %d\n", errno);  
  123.         exit( EXIT_FAILURE );  
  124.     }  
  125.       
  126.     for( i = 0; i < CHILD_NUM; i++ )     //!> 创建10个线程   
  127.     {  
  128.         if( pthread_create( &tids[i], NULL, thread_do, &listen_fd ) != 0 )  
  129.         {  
  130.             printf("Create %d thread error! : %d", i, errno);  
  131.             exit( EXIT_FAILURE );  
  132.         }  
  133.           
  134.         if(  pthread_detach( tids[i] ) != 0 )  
  135.         {  
  136.             printf("Thead %d detach error! :%d \n", ( int )pthread_self(), errno);  
  137.             exit( EXIT_FAILURE );  
  138.         }  
  139.           
  140.     }  
  141.   
  142.     while( 1 )  
  143.     {  
  144.         pause();                //!> 主进程暂停   
  145.     }  
  146.       
  147.     return 0;  
  148. }  

到此,全部结束!

linux
相关资讯       Linux服务器 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊��网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款