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 连接端口有信息
{