//read操作加上超时时间。
1 int read_timeout(int fd, void *buf, uint32_t count, int time)
{
if(time > ) {
fd_set rSet;
FD_ZERO(&rSet);
FD_SET(fd, &rSet); struct timeval timeout;
memset(&timeout, , sizeof(timeout));
timeout.tv_sec = time;
timeout.tv_usec = ; int ret;
while() {
ret = select(fd+, &rSet, NULL, NULL, &timeout);
if(ret < ) {
if(errno == EINTR) continue;
ERR_EXIT("select");
} else if(ret == ) {
errno = ETIMEDOUT;
return -;
} else {
break;
}
}
}
int readNum;
readNum = read(fd, buf, count);
return readNum;
}

写超时

 int write_timeout(int fd, void *buf, uint32_t count, int time)
{
if(time > ) {
fd_set wSet;
FD_ZERO(&wSet);
FD_SET(fd, &wSet); struct timeval timeout;
memset(&timeout, , sizeof(timeout));
timeout.tv_sec = time;
timeout.tv_usec = ; int ret;
while() {
ret = select(fd+, NULL, &wSet, NULL, &timeout);
if(ret < ) {
if(errno == EINTR) continue;
ERR_EXIT("select");
} else if(ret == ) {
errno = ETIMEDOUT;
return -;
} else {
break;
}
}
}
int writeNum;
writeNum = write(fd, buf, count);
return writeNum;
}

accept超时操作

int accept_timeout(int fd, struct sockaddrin *addr, socklen_t *addrlen, int time)
{
int ret;
if(time > ) {
fd_set rSet;
FD_ZERO(&rSet);
FD_SET(fd, &rSet); struct timeval timeout;
timeout.tv_sec = time;
timeout.tv_usec = ; int selectRet;
do {
selectRet = select(fd + , &rSet, NULL, NULL, &timeout);
}while(selectRet < && selectRet == EINTR);
if(selectRet < ) {
return -;
} else if(selectRet == ) {
errno = ETIMEDOUT;
return -;
}
}
if(addr) {
ret = accept(fd, (struct sockaddr *)addr, addrlen);
} else {
ret = accept(fd, NULL, NULL);
}
return ret;
}

检测监听套接字是否可读,当监听套接字可读的时候,就认为连接队列发生了连接。

connect

 void setNonBlockMode(int fd)
{
int flags = fcntl(fd, F_GETFL);
if(flags < ) {
ERR_EXIT("fcntl");
}
flags |= O_NONBLOCK;
if(fcntl(fd, F_SETFL, flags) < ) {
ERR_EXIT("fcntl");
}
} void setBlockMode(int fd)
{
int flags = fcntl(fd, F_GETFL);
if(flags < ) {
ERR_EXIT("fcntl");
}
flags &= ~O_NONBLOCK;
if(fcntl(fd, F_SETFL, flags) < ) {
ERR_EXIT("fcntl");
} } int connect_timeout(int sockfd, struct sockaddrin *addr, socklen_t addrlen, int time)
{
int ret = -; if(time > ) {
setNonBlockMode(sockfd);
}
ret = connect(sockfd, (struct sockaddr*)addr, addrlen);
if(ret < && errno == EINPROGRESS) {
fd_set wSet;
FD_ZERO(&wSet);
FD_SET(sockfd, &wSet); struct timeval timeout;
timeout.tv_sec = time;
timeout.tv_usec = ; int selcetRet;
do{
selcetRet = select(sockfd + , NULL, &wSet, NULL, &timeout);
}while(selcetRet < && errno == EINTR);
if(selcetRet < ) {
ret = -;
} else if(selcetRet == ) {
ret = -;
errno = ETIMEDOUT;
} else if(selcetRet > ) {
int err;
socklen_t socklen = sizeof(err);
int sockoptRet = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, &socklen);
if(sockoptRet == -) {
ret = -;
}
if(err == ) {
ret = ;
} else {
errno = err;
ret = -;
}
}
}
if(time > ) {
setBlockMode(sockfd);
}
return ret;
}

1.设置fd为非阻塞模式。

2.调用connect操作,如果网络条件很好,比如本机两个socket发生连接,会发生成功返回。

3.正常情况下,connect立即返回-1并设置errno为EINPROGRESS 表示正在连接过程中。

4.使用select检测fd是否可写。 如果检测到可写也有如下两种情况:

  1. connect连接成功。

  2. 产生了错误,我们就需要用getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, &socklen); 来判断是否发生了错误。

read,write,accept,connect 超时封装的更多相关文章

  1. C Socket编程之Connect超时 (转)

    网络编程中socket的分量我想大家都很清楚了,socket也就是套接口,在套接口编程中,提到超时的概念,我们一下子就能想到3个:发送超时,接收超时,以及select超时(注:select函数并不是只 ...

  2. VC socket Connect 超时时间设置

    设置connect超时很简单,CSDN上也有人提到过使用select,但却没有一个令人满意与完整的答案.偶所讲的也正是select函数,此函数集成在winsock1.1中,简单点讲,"作用使 ...

  3. 设置linux中tcp默认的20秒connect超时时间(转)

    无论你用任何语言或者是网络库,你都可以设置网络操作的超时时间,特别是connect.read.write的超时时间. 你可以在代码中把超时时间设置任意大小值,但是connect方法会有一点特殊. co ...

  4. linux下connect超时时间探究

    最近在linux做服务器开发的时候,发现了一个现象:服务器在启动的时候调用了 connect 函数,因为连接了一个不可用的端口,导致connect最后报出了 “Connection timed out ...

  5. linux 设置connect 超时代码[select/epoll]

    转载请注明来源:https://www.cnblogs.com/hookjc/ linux下socket编程有常见的几个系统调用: 对于服务器来说, 有socket(), bind(),listen( ...

  6. [转]windows下设置socket的connect超时

    原文地址:http://www.cnblogs.com/BloodAndBone/archive/2012/05/22/2513338.html 变相的实现connect的超时,我要讲的就是这个方法, ...

  7. iOS 设置connect超时

    NSLock *theLock; [theLock lock]; int fd, error; struct sockaddr_in addr; ))<) { cout<<" ...

  8. Linux下connect超时处理【总结】

    1.前言 最近在写一个测试工具,要求快速的高效率的扫描出各个服务器开放了哪些端口.当时想了一下,ping只能检测ip,判断服务器的网络是连通的,而不能判断是否开放了端口.我们知道端口属于网络的应用层, ...

  9. Socket相关函数(1)- socket(), bind(), listen(), accept(), connect(), TCP模型

    tcp_server.c #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #in ...

随机推荐

  1. python(23)- 面向对象简单介绍

    面向概述 面向过程:根据业务逻辑从上到下写垒代码 面向过程的设计的核心是过程,过程即解决问题的步骤, 面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西 优点:极大降低了程序的复杂 ...

  2. xhtml三种元素类型

    xhtml三种元素类型:块级元素/内联元素/可变元素 块级元素:独占一行.可自定义自己的宽度和高度.作为其他元素的容器,可容纳其他内联元素和块级元素,喻做一个盒子.内联元素:始终以行内逐个显示.不能设 ...

  3. 在eclipse中查找指定文件 [多种方法]

    在eclipse中查找指定文件   1.ctrl+h打开搜索界面 File Search: containing text填*,File name patterns填写hello.*,可以找到hell ...

  4. 关于0基础磁盘管理(gpt UEFI...)最好的一篇文章(来自gentoo linux)

    放链接:https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Disks 顺便几张图 watermark/2/text/aHR0cDovL2 ...

  5. Sublime Text 3相关配置和设置

    Sublime Text 3打开txt中文乱码的解决方法 Sublime Text是一个很强大的编辑器,但是对中文的支持并不好,在Sublime Text 2 时,能够通过命令行的方式安装编码包来解决 ...

  6. Nginx简单了解

    1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 配置: server { listen80; # 端口号 lo ...

  7. kubernetes之初始容器(init container)

    系列目录 理解初始容器 一个pod里可以运行多个容器,它也可以运行一个或者多个初始容器,初始容器先于应用容器运行,除了以下两点外,初始容器和普通容器没有什么两样: 它们总是run to complet ...

  8. kubernetes调度之污点(taint)和容忍(toleration)

    系列目录 节点亲和性(affinity),是节点的一种属性,让符合条件的pod亲附于它(倾向于或者硬性要求).污点是一种相反的行为,它会使pod抗拒此节点(即pod调度的时候不被调度到此节点) 污点和 ...

  9. C语言预处理条件语句的 与或运算

    1.#ifdef 与或运算 #ifdef  (MIN)  && (MAX)  ----------------------------错误使用 #if  defined(MIN)  & ...

  10. MVC——分页

    添加类PageBar.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; ...