广告
返回顶部
首页 > 资讯 > 操作系统 >在linux下玩转带有超时时间的connect函数
  • 179
分享到

在linux下玩转带有超时时间的connect函数

摘要

在之前的文章中,我们在windows下玩过带有超时时间的,本文我们在linux下来玩。在某次面试中,还被遇到了这个问题,有意思。 直接上客户端代码: #include <unistd.h> #includ

在之前的文章中,我们在windows下玩过带有超时时间的,本文我们在linux下来玩。在某次面试中,还被遇到了这个问题,有意思。

直接上客户端代码:


#include <unistd.h>
#include <sys/types.h>
#include <sys/Socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <time.h>
int main(int arGC, char *argv[]) // 注意输入参数, 带上ip和port
{
  int sockClient = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in addrSrv;
  addrSrv.sin_addr.s_addr = inet_addr(argv[1]);
  addrSrv.sin_family = AF_INET;
  addrSrv.sin_port = htons(atoi(argv[2]));
 fcntl(sockClient, F_SETFL, fcntl(sockClient, F_GETFL, 0)|O_NONBLOCK); 
  int iRet = connect(sockClient, ( const struct sockaddr *)&addrSrv, sizeof(struct sockaddr_in));
 printf("connect iRet is %d, errmsg:%s\n", iRet, strerror(errno)); // 返回-1不一定是异常
 if (iRet != 0) 
 { 
   if(errno != EINPROGRESS)
 {
  printf("connect error:%s\n", strerror(errno)); 
 }
   else 
 {
  struct timeval tm = {5, 0}; 
  fd_set wset, rset; 
  FD_ZERO(&wset); 
  FD_ZERO(&rset); 
  FD_SET(sockClient, &wset); 
  FD_SET(sockClient, &rset); 
  int time1 = time(NULL);
  int n = select(sockClient + 1, &rset, &wset, NULL, &tm); 
  int time2 = time(NULL);
  printf("time gap is %d\n", time2 - time1);
  if(n < 0) 
  { 
   printf("select error, n is %d\n", n); 
  } 
  else if(n == 0) 
  { 
   printf("connect time out\n"); 
  } 
  else if (n == 1) 
  {
   if(FD_ISSET(sockClient, &wset)) 
   { 
    printf("connect ok!\n"); 
    fcntl(sockClient, F_SETFL, fcntl(sockClient, F_GETFL, 0) & ~O_NONBLOCK); 
   } 
   else 
   { 
    printf("unknow error:%s\n", strerror(errno)); 
   } 
  }
  else
  {
  printf("oh, not care now, n is %d\n", n);
  }
 } 
 } 
 printf("I am here!\n");
  getchar();
  close(sockClient);
  return 0;
}

服务端代码,我们已经写过多次,本文就不写了。

测试,上述程序OK, 用tcpdump抓包,还能学到不少东西,比如SYN包重传,RST包等。有点意思。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接

--结束END--

本文标题: 在linux下玩转带有超时时间的connect函数

本文链接: https://www.lsjlt.com/news/20240.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 在linux下玩转带有超时时间的connect函数
    在之前的文章中,我们在Windows下玩过带有超时时间的,本文我们在linux下来玩。在某次面试中,还被遇到了这个问题,有意思。 直接上客户端代码: #include <unistd.h> #includ...
    99+
    2022-06-04
    linux下connect函数 在linux下玩转带有超时时间的connect函数
  • 在Linux/Mac下怎么为Python函数添加超时时间
    本篇内容主要讲解“在Linux/Mac下怎么为Python函数添加超时时间”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“在Linux/Mac下怎么为Python函数添加超时时间”吧!首先我们来看...
    99+
    2023-06-16
  • linux下与时间有关的函数和结构体是什么
    本篇内容介绍了“linux下与时间有关的函数和结构体是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!时间类型。Linux下常用的时间类型...
    99+
    2023-06-04
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作