|
我的服务器端线程如下,发现客户端的连接可以感知到,但是客户端发送的信息却收不到,我发现只有有客户端连接时select函数才有结果返回否则程序就阻塞到select处不再往下运行了,请问这是什么原因?
void * ESMALoginService::ListenNotify(void * a_Para)
{
int fd = 0;
int len = 0;
int skt;
fd_set readfds;
char buffer[2048];
char test[]="------------------------welcome in control-server---------------------\n";
ESMALoginService * cs = static_cast<ESMALoginService*>(a_Para);
while(1)
{
cout<<"start loop"<<endl;
struct sockaddr_in addr;
FD_ZERO(&readfds);
FD_SET(cs->sktfd, &readfds);
for(fd=0; fd<MAX_CONNECT_NUM; fd++)
{
if(cs->sktConn[fd].flag)
FD_SET(fd, &readfds);
}
cout<<"select start:"<<endl;
int sel = select(MAX_CONNECT_NUM, &readfds, NULL, NULL, NULL);
cout<<"select ret:"<<sel<<endl;
if(!sel)
continue;
cout<<"Select ........."<<endl;
for(fd=0; fd<MAX_CONNECT_NUM; fd++)
{
//cout<<"ISSET........"<<endl;
if(FD_ISSET(fd, &readfds))
{
cout<<"connect........."<<endl;
if(cs->sktfd == fd)
{
skt=accept(cs->sktfd,(struct sockaddr*)&addr,(socklen_t*)&len);
if (skt >= 0)
{
cs->sktConn[skt].skt = skt;
cs->sktConn[skt].port = ntohs(addr.sin_port);
strcpy((char*)cs->sktConn[skt].IP,inet_ntoa(addr.sin_addr));
cout<<"-------Client connect to "<<inet_ntoa(addr.sin_addr);
cout<<" port ="<<ntohs(addr.sin_port)<<"---------"<<endl;
if(send(skt,test,sizeof(test), MSG_NOSIGNAL) < 0)
perror("send faile");
}
}
else
{
cout<<"Receive data"<<endl;
memset(buffer, 0, sizeof(buffer));
int res = read(fd, buffer, sizeof(buffer));
if(res <= 0)
{
printf("connection closed.%s:%d\n", cs->sktConn[skt].IP, cs->sktConn[skt].port);
cs->sktConn[skt].flag=0;
close(fd);
}
else
{
cout<<buffer<<endl;
}
} //if(cs->socketfd == fd)
}//if(FD_ISSET(fd, &readfds))
}//fro
} //while
} |
|