自由空间
free space
<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

留言簿(22)

随笔分类

随笔档案

文章档案

相册

vckbase

搜索

最新评论

阅读排行榜

评论排行榜

 
VC知识库BLOG   首页  新随笔  联系  聚合  登录 
  随笔-81 文章-1 评论-228 Trackbacks-0

没有考虑到read的返回值:P
/*
 brief:  超时读数据
 fd:  文件描述述
 lpdata:  数据指针
 timeout: 超时值(秒)
 len:  读数据长度 
return:实际读数据长度
*/
int ReadData(int fd,char *lp_out_data,int timeout,int len)
{
 fd_set rxset;
 int z;
 int nfds;
int sumlen =0;
 struct timeval tv;
 char lpdata[RECV_MAX_DATA];
 do
 {
  FD_ZERO(&rxset);
  if(fd>=0)
   FD_SET(fd,&rxset);
  nfds = fd+1;
  tv.tv_sec=timeout;
  tv.tv_usec = 0;
  do
  {
   z = select(nfds,&rxset,0,0,&tv);
  }while(z==-1&&errno==EINTR);
  if(z == -1)
   printf("select(2)\n");
  if(z == 0)
  {
   printf("timeout\n");
   fd = -1;
  }
  
  if(fd>=0&&FD_ISSET(fd,&rxset))
  {
   memset(lpdata,0,sizeof(lpdata));
   if (len > RECV_MAX_DATA)
    len = RECV_MAX_DATA;
   z = read(fd,lpdata,len);
   if(z == -1)
   {
    printf("read(2) of fd\n");
    fd = -1;
   }
   if(z >0)
   {
    sumlen = sumlen+z;
    lpdata[z]=0;
    strcat(lp_out_data,lpdata);
//    printf("read %d bytes <<%02x>> from fd;\n",z,lpdata[0]);
   }
   else
   {
    printf("read EOF from fd;");
    fd = -1;
   }
  }
 }while (fd>=0);
return sumlen;

}

posted on 2005-02-22 14:08 自由空间 阅读(4446) 评论(6)  编辑 收藏
Comments
  • # re: bug修改
    爱立信
    Posted @ 2005-02-23 16:40
    嘿嘿!!!!!!!!!!!!!!!
  • # I find that we can use function——recv directly
    清风雨
    Posted @ 2005-04-21 14:23
    send等都可以直接使用。 不需要非得select吧? 我一直很奇怪呢。(我不需要他的 多路复用 能力)
  • # re: bug修改
    freedk
    Posted @ 2005-04-26 10:39
    send函数可以直接超时吗?
    ssize_t send(int s,const void*buf,size_t len,int flags)

    如果你不用select肯定不能超时的,那只是socket系统的超时。
    int select (int n,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,struct timeval*timeout)
  • # dgdgd
    gdgdgd
    Posted @ 2006-02-23 03:37

    收鸟收鸟~~~申请使用~~~~~ 
  • # re: bug修改
    gdgdgd
    Posted @ 2006-02-23 03:37

    收鸟收鸟~~~申请使用~~~~~ 
  • # re: bug修改
    gdgdgd
    Posted @ 2006-02-23 03:37
    dsefsfsf
标题  
姓名  
主页
验证码 *
内容   
  登录  使用高级评论  Top
[使用Ctrl+Enter键可以直接提交]