当前位置: 技术问答>linux和unix
高手看看:select问题
来源: 互联网 发布时间:2015-09-15
本文导语: 程序如下; #include #include #include #include #include #include #include #include #include #include #include #include #define PORT 8801 /*监听端口号*/ main(void) { int sockfd,new_fd; ...
程序如下;
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8801 /*监听端口号*/
main(void)
{
int sockfd,new_fd; /*socket描述符*/
struct sockaddr_in server_addr; /*socket结构*/
struct sockaddr_in client_addr; /*socket结构*/
int sin_size,portnumber=PORT;
pthread_t id;
int i,ret;
char hello[]="Hello! World!n";
/* 服务器端开始建立socket描述符 */
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"Socket error:%sna",strerror(errno));
exit(1);
}
/* 服务器端填充 sockaddr结构 */
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(portnumber);
/* 捆绑sockfd描述符 */
if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Bind error:%sna",strerror(errno));
exit(1);
}
/* 监听sockfd描述符 */
if(listen(sockfd,5)==-1)
{
fprintf(stderr,"Listen error:%sna",strerror(errno));
exit(1);
}
/* 服务器阻塞,直到客户程序建立连接 */
sin_size=sizeof(struct sockaddr_in);
if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1)
{
fprintf(stderr,"Accept error:%sna",strerror(errno));
exit(1);
}
fprintf(stderr,"Server get connection from %sn",inet_ntoa(client_addr.sin_addr));
int err;
fd_set fdR;
struct timeval timeout ;
timeout.tv_sec=10;
timeout.tv_usec=0;
while(1) {
FD_ZERO(&fdR);
FD_SET(new_fd, &fdR);
err=select(new_fd + 1, &fdR, NULL, NULL, &timeout);
if(err==-1){
printf("error!n");
break; /*error handled by u; */
}
else if(err==0){
printf("timeout!n");/*timeout hanled by u;*/
}
else
if (FD_ISSET(*fd,&fdR)) {
printf("read or rev!n");
}
}
close(new_fd);
close(sockfd);
exit(0);
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8801 /*监听端口号*/
main(void)
{
int sockfd,new_fd; /*socket描述符*/
struct sockaddr_in server_addr; /*socket结构*/
struct sockaddr_in client_addr; /*socket结构*/
int sin_size,portnumber=PORT;
pthread_t id;
int i,ret;
char hello[]="Hello! World!n";
/* 服务器端开始建立socket描述符 */
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"Socket error:%sna",strerror(errno));
exit(1);
}
/* 服务器端填充 sockaddr结构 */
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(portnumber);
/* 捆绑sockfd描述符 */
if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Bind error:%sna",strerror(errno));
exit(1);
}
/* 监听sockfd描述符 */
if(listen(sockfd,5)==-1)
{
fprintf(stderr,"Listen error:%sna",strerror(errno));
exit(1);
}
/* 服务器阻塞,直到客户程序建立连接 */
sin_size=sizeof(struct sockaddr_in);
if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1)
{
fprintf(stderr,"Accept error:%sna",strerror(errno));
exit(1);
}
fprintf(stderr,"Server get connection from %sn",inet_ntoa(client_addr.sin_addr));
int err;
fd_set fdR;
struct timeval timeout ;
timeout.tv_sec=10;
timeout.tv_usec=0;
while(1) {
FD_ZERO(&fdR);
FD_SET(new_fd, &fdR);
err=select(new_fd + 1, &fdR, NULL, NULL, &timeout);
if(err==-1){
printf("error!n");
break; /*error handled by u; */
}
else if(err==0){
printf("timeout!n");/*timeout hanled by u;*/
}
else
if (FD_ISSET(*fd,&fdR)) {
printf("read or rev!n");
}
}
close(new_fd);
close(sockfd);
exit(0);
}
|
你的程序有2个地方有问题
timeout.tv_sec=10;
timeout.tv_usec=0;
要放在 while() 循环内部,select执行完毕会清空 timeout
if (FD_ISSET(*fd,&fdR)) {
printf("read or rev!n");
}
你必须read以下*fd,将数据取走,否则内核不会请空标志位
timeout.tv_sec=10;
timeout.tv_usec=0;
要放在 while() 循环内部,select执行完毕会清空 timeout
if (FD_ISSET(*fd,&fdR)) {
printf("read or rev!n");
}
你必须read以下*fd,将数据取走,否则内核不会请空标志位