Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32232
  • 博文数量: 2
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 110
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-12 15:00
文章分类

全部博文(2)

文章存档

2011年(1)

2010年(1)

我的朋友
最近访客

分类: C/C++

2010-03-12 14:12:23

int sendn_select(int fd, const char *buf, unsigned int len)
{
        fd_set wset;
        int ok = 0;
        int send_len = 0;
        int tmp_len = 0;
        struct timeval t;
        int maxfd;

        if(fd < 0 || !buf || !len)
        {
                return -1;
        }

        FD_ZERO(&wset);
        maxfd = fd + 1;

        while (!ok)
        {
                t.tv_sec = 0;
                t.tv_usec = 100;

                FD_SET(fd,&wset);
                if(select(maxfd,NULL,&wset,NULL,&t) < 0)
                {
                        if(errno == EINTR)
                        {
                                continue;
                        }
                        else
                        {
                                return -1;
                        }
                }
                tmp_len = send(fd, buf + send_len, len - (unsigned int)send_len, 0);
                if (!tmp_len)
                {
                        continue;
                }
                else if (tmp_len < 0)
                {
                        if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK)
                        {
                                return -1;
                        }
                }
                else if (tmp_len < (int)len - send_len)
                {
                        send_len += tmp_len;
                }
                else {
                        ok = 1;
                }
        }
        return 0;
}

阅读(1290) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:博客已升级,请注意变更地址

给主人留下些什么吧!~~