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

Linux 网络编程---->多路复用:select实例!

[日期:2012-03-22] 来源:Linux社区  作者:shanshanpt [字体:大 中 小]
//!> client端代码
//!>client.c


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/select.h>

#define MAXLINE 1024
#define SERV_PORT 6000

//!> 注意输入是由stdin,接受是由server发送过来
//!> 所以在client端也是需要select进行处理的
void send_and_recv( int connfd )
{
    FILE * fp =stdin;
   int   lens;
    charsend[MAXLINE];
    charrecv[MAXLINE];
    fd_setrset;
    FD_ZERO(&rset );
    int maxfd =( fileno( fp ) > connfd ? fileno( fp ) :connfd  + 1);   
                                    //!> 输入和输出的最大值
    int n;
   
    while( 1)
    {
       FD_SET(fileno( fp ), &rset );
       FD_SET(connfd, &rset);         //!> 注意不要把rset看作是简单的一个变量
                                    //!> 注意它其实是可以包含一组套接字的哦,
                                    //!> 相当于是封装的数组!每次都要是新的哦!
      
       if( select(maxfd, &rset, NULL, NULL, NULL ) == -1 )
       {
         printf("Client Select Error..\n");
         exit(EXIT_FAILURE  );
       }
      
      //!> if 连接口有信息
       if(FD_ISSET( connfd, &rset ))   //!> if 连接端口有信息
       {
          printf("client get from server ...\n" );
          memset(recv, 0, sizeof( recv ) );
          n = read(connfd, recv, MAXLINE );
          if( n == 0)
          {
             printf("Recvok...\n");
             break;
          }
          else if( n== -1 )
          {
             printf("Recverror...\n");
             break;
          }
          else
          {
             lens =strlen( recv );
             recv[lens] ='\0';
            //!> 写到stdout
             write(STDOUT_FILENO, recv, MAXLINE );
            printf("\n");
          }

       }
      
      //!> if 有stdin输入
       if(FD_ISSET( fileno( fp ), &rset ))   //!> if 有输入
       {
         //!> printf("client stdin ...\n");
         
          memset(send, 0, sizeof( send ) );
          if( fgets(send, MAXLINE, fp ) == NULL )
          {
            printf("End...\n");
             exit(EXIT_FAILURE );
          }
          else
          {
            //!>if( str )
             lens =strlen( send );
             send[lens-1]= '\0';      //!> 减一的原因是不要回车字符
                                 //!> 经验值:这一步非常重要的哦!!!!!!!!
             if( strcmp(send, "q" ) == 0 )
             {
                printf("Bye..\n" );
               return;
             }
            
            printf("Client send : %s\n", send);
             write(connfd, send, strlen( send ) );
          }
       }
      
    }
   
}

int main( int argc, char ** argv )
{
   //!> char * SERV_IP = "10.30.97.188";
    char   buf[MAXLINE];
   int     connfd;
    structsockaddr_in servaddr;
   
    if( argc !=2 )
    {
      printf("Input server ip !\n");
       exit(EXIT_FAILURE );
    }
   
   //!> 建立套接字
    if( ( connfd= socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )
    {
      printf("Socket Error...\n" , errno );
       exit(EXIT_FAILURE );
    }

   //!> 套接字信息
   bzero(&servaddr, sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_port = htons(SERV_PORT);
   inet_pton(AF_INET, argv[1],&servaddr.sin_addr);
   
   //!> 链接server
    if( connect(connfd, ( struct sockaddr * )&servaddr, sizeof( servaddr ) ) < 0)
    {
      printf("Connect error..\n");
      exit(EXIT_FAILURE);
   }   
   

   //!>
   //!> send and recv
   send_and_recv( connfd );
   
   //!>

    close(connfd );
   printf("Exit\n");
   
    return0;
}


编译+运行:

gcc -o server server.c
gcc -o client   client.c

./server
./client10.80.1.251    (注意参数你指定的server的IP哦  )


如果要多个一起运行,给个脚本:
#!/bin/sh
index=10

while [ $index -gt 0 ]
do
    ./client  10.80.1.251&         # 注意在后台哦,所以只能看到在server下的现象,可以自己改进
     $index = `expr $index - 1`
done

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

       

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