日期:2014-05-16  浏览次数:20723 次

socket编程使用select的问题?
一个简单的程序:
Server端使用select监听多个Client,Client发数据给Server,Server把受到的数据打印出来。
出现的问题是:当只有一个Client连接时,无法收到Client端发来的数据,多个Client连接时则正常。
我是使用telnet做Client来测试的。
代码如下,各位老大帮看一下:
#include   <stdio.h>
#include   <string.h>
#include   <stdlib.h>
#include   <strings.h>
#include   <sys/select.h>
#include   <netdb.h>
#include   <sys/socket.h>
#include   <netinet/in.h>
#include   <arpa/inet.h>

#define   BUF_SIZ   1024
int   port   =   2007;

int   main()
{
/*   local   varibles   */
int   srv_skt_fd;
int   clt_skt_fd;
fd_set   set,read_set;
int   max_fd;
int   i;

char   buf[BUF_SIZ];
int   len;

struct   sockaddr_in   serv_addr;
struct   sockaddr_in   clt_addr;

/*   program   begin   from   here   */
/*   Step1:   open   a   socket   */
if   ((srv_skt_fd   =   socket(AF_INET,SOCK_STREAM,0))   ==   -1)/*   TCP   socket   */
{
perror( "socket   open ");
exit(1);
}

/*   Step2:   bind   it   to   host   address   */
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sin_family   =   AF_INET;
serv_addr.sin_addr.s_addr   =   htonl(INADDR_ANY);
serv_addr.sin_port   =   htons(port);
if   (bind(srv_skt_fd,(struct   sockaddr*)&serv_addr,sizeof(serv_addr))   ==   -1)
{
perror( "bind ");
close(srv_skt_fd);
exit(1);
}

/*   Step3:   listen   */
if   (listen(srv_skt_fd,SOMAXCONN)   ==   -1)
{
perror( "listen ");
close(srv_skt_fd);
exit(1);
}

/*   Step4:   build   a   fd_set   for   select   */
FD_ZERO(&set);

/*   Step5:   add   server   socket   fd   to   this   fd_set   */
FD_SET(srv_skt_fd,&set);
max_fd   =   srv_skt_fd   +   1;

/*   Step6:   write   a   infinite   loop   that   call   select   and   handle   all   requests   */
while   (1)
{
printf( "start\n ");
read_set   =   set;
/*   wait   for   a   request   here   */
if   (select(max_fd,&read_set,NULL,NULL,NULL)   ==   -1)
{
perror( "select ");
close(srv_skt_fd);
exit(1);
}
printf( "after   select\n ");
/*   cleck   which   one   is   coming   */
for   (i   =   0;i   <   max_fd;i++)
{
/*   Step7:   case   1:if   the   request   is   from   the   server   socket   fd,
  then   accept   it   and   add   this   new   socket   fd   to   fd_set
              case   2:if   the   requeset   is   from   a   client,   handle   it;
*/
if   (FD_ISSET(i,&read_set))
{
/* &nbs