超时设置

//read操作加上超时时间。
 1 int read_timeout(int fd, void *buf, uint32_t count, int time)
 2 {
 3     if(time > 0) {
 4         fd_set rSet;
 5         FD_ZERO(&rSet);
 6         FD_SET(fd, &rSet);
 7 
 8         struct timeval timeout;
 9         memset(&timeout, 0, sizeof(timeout));
10         timeout.tv_sec = time;
11         timeout.tv_usec = 0;
12         
13         int ret;
14         while(1) {
15             ret = select(fd+1, &rSet, NULL, NULL, &timeout);
16             if(ret < 0) {
17                 if(errno == EINTR) continue;
18                 ERR_EXIT("select");
19             } else if(ret == 0) {
20                 errno = ETIMEDOUT;
21                 return -1;
22             } else {
23                 break;
24             }
25         }
26     }
27     int readNum;
28     readNum = read(fd, buf, count);
29     return readNum;
30 }

写超时

 1 int write_timeout(int fd, void *buf, uint32_t count, int time)
 2 {
 3     if(time > 0) {
 4         fd_set wSet;
 5         FD_ZERO(&wSet);
 6         FD_SET(fd, &wSet);
 7 
 8         struct timeval timeout;
 9         memset(&timeout, 0, sizeof(timeout));
10         timeout.tv_sec = time;
11         timeout.tv_usec = 0;
12         
13         int ret;
14         while(1) {
15             ret = select(fd+1, NULL, &wSet, NULL, &timeout);
16             if(ret < 0) {
17                 if(errno == EINTR) continue;
18                 ERR_EXIT("select");
19             } else if(ret == 0) {
20                 errno = ETIMEDOUT;
21                 return -1;
22             } else {
23                 break;
24             }
25         }
26     }
27     int writeNum;
28     writeNum = write(fd, buf, count);
29     return writeNum;
30 }

accept超时操作

int accept_timeout(int fd, struct sockaddrin *addr, socklen_t *addrlen, int time)
{
  int ret;
  if(time > 0) {
    fd_set rSet;
    FD_ZERO(&rSet);
    FD_SET(fd, &rSet);

    struct timeval timeout;
    timeout.tv_sec = time;
    timeout.tv_usec = 0;

    int selectRet;
    do {
      selectRet = select(fd + 1, &rSet, NULL, NULL, &timeout);
    }while(selectRet < 0 && selectRet == EINTR);
    if(selectRet < 0 ) {
      return -1;
    } else if(selectRet == 0) {
      errno = ETIMEDOUT;
      return -1;
    }	
  }
  if(addr) {
    ret = accept(fd, (struct sockaddr *)addr, addrlen);
  } else {
    ret = accept(fd, NULL, NULL);
  }
  return ret;
}

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

connect

 1 void setNonBlockMode(int fd)
 2 {
 3     int flags = fcntl(fd, F_GETFL);
 4     if(flags < 0) {
 5         ERR_EXIT("fcntl");
 6     }
 7     flags |= O_NONBLOCK;
 8     if(fcntl(fd, F_SETFL, flags) < 0) {
 9         ERR_EXIT("fcntl");
10     }
11 }
12 
13 void setBlockMode(int fd)
14 {
15     int flags = fcntl(fd, F_GETFL);
16     if(flags < 0) {
17         ERR_EXIT("fcntl");
18     }
19     flags &= ~O_NONBLOCK;
20     if(fcntl(fd, F_SETFL, flags) < 0) {
21         ERR_EXIT("fcntl");
22     }
23 
24 }
25 
26 int connect_timeout(int sockfd, struct sockaddrin *addr, socklen_t addrlen, int time)
27 {
28     int ret = -1;
29 
30     if(time > 0) {
31         setNonBlockMode(sockfd);
32     }    
33     ret = connect(sockfd, (struct sockaddr*)addr, addrlen);
34     if(ret < 0 && errno == EINPROGRESS) {
35         fd_set wSet;
36         FD_ZERO(&wSet);
37         FD_SET(sockfd, &wSet);
38         
39         struct timeval timeout;
40         timeout.tv_sec = time;
41         timeout.tv_usec = 0;
42         
43         int selcetRet;
44         do{
45             selcetRet = select(sockfd + 1, NULL, &wSet, NULL, &timeout);
46         }while(selcetRet < 0 && errno == EINTR);
47         if(selcetRet < 0) {
48             ret = -1;
49         } else if(selcetRet == 0) {
50             ret = -1;
51             errno = ETIMEDOUT;
52         } else if(selcetRet > 0) {
53             int err;
54             socklen_t socklen = sizeof(err);
55             int sockoptRet = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, &socklen);
56             if(sockoptRet == -1) {
57                 ret = -1;
58             } 
59             if(err == 0) {
60                 ret = 0;
61             } else {
62                 errno = err;
63                 ret = -1;
64             }
65         }
66     }
67     if(time > 0) {
68         setBlockMode(sockfd);
69     }
70     return ret;
71 }

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

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

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

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

1. connect连接成功。

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

在使用 OKHttp 时,合理设置超时参数非常重要,以避免网络请求卡死用户体验变差。OKHttp 提供了三种主要的超时设置: ### 1. 连接超时(Connect Timeout) 建立与目标服务器的连接的最大时间。 ### 2. 读取超时(Read Timeout) 从服务器读取响应数据的最大时间。 ### 3. 写入超时(Write Timeout) 向服务器写入请求数据的最大时间。 --- ### 示例代码(Java): ```java OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(30, TimeUnit.SECONDS) // 连接超时 .readTimeout(30, TimeUnit.SECONDS) // 读取超时 .writeTimeout(30, TimeUnit.SECONDS) // 写入超时 .build(); ``` --- ### Kotlin 示例: ```kotlin val client = OkHttpClient.Builder() .connectTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) .build() ``` 你也可以为每个请求单独设置超时: ```java Request request = new Request.Builder() .url("https://example.com") .build(); // 设置单个请求的超时(通过 Dispatcher 异步处理时) ``` --- ### 建议值: - **开发阶段**:可设为较长时间(如 60s),便于调试。 - **生产环境**:建议设置为 10s~30s,根据网络环境和业务需求调整。 - 对于移动端应用,建议适当增加超时时间以适应不稳定的网络。 --- ### 注意事项: - 超时时间太短可能导致频繁超时失败; - 超时时间太长可能影响用户体验资源释放; - 可结合重试机制使用,提高健壮性; - 使用 `Interceptor` 可以实现更复杂的超时控制逻辑。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值