婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > 在linux下玩轉(zhuǎn)帶有超時(shí)時(shí)間的connect函數(shù)

在linux下玩轉(zhuǎn)帶有超時(shí)時(shí)間的connect函數(shù)

熱門(mén)標(biāo)簽:金融行業(yè)外呼線路 江蘇電銷外呼防封系統(tǒng)是什么 東莞人工智能電銷機(jī)器人供應(yīng)商 賀州市地圖標(biāo)注app 長(zhǎng)沙開(kāi)福怎么申請(qǐng)400電話 廣州電銷機(jī)器人系統(tǒng)圖 百度地圖標(biāo)注要不要錢(qián) 高德地圖標(biāo)注無(wú)營(yíng)業(yè)執(zhí)照 智能電話機(jī)器人線路

在之前的文章中,我們?cè)赪indows下玩過(guò)帶有超時(shí)時(shí)間的,本文我們?cè)趌inux下來(lái)玩。在某次面試中,還被遇到了這個(gè)問(wèn)題,有意思。

直接上客戶端代碼:

#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[]) // 注意輸入?yún)?shù), 帶上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;
}

服務(wù)端代碼,我們已經(jīng)寫(xiě)過(guò)多次,本文就不寫(xiě)了。

經(jīng)測(cè)試,上述程序OK, 用tcpdump抓包,還能學(xué)到不少東西,比如SYN包重傳,RST包等。有點(diǎn)意思。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

標(biāo)簽:廊坊 永州 玉樹(shù) 洛陽(yáng) 北京 張家界 滄州 松原

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《在linux下玩轉(zhuǎn)帶有超時(shí)時(shí)間的connect函數(shù)》,本文關(guān)鍵詞  在,linux,下玩轉(zhuǎn),帶有,超,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《在linux下玩轉(zhuǎn)帶有超時(shí)時(shí)間的connect函數(shù)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于在linux下玩轉(zhuǎn)帶有超時(shí)時(shí)間的connect函數(shù)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 东辽县| 岐山县| 石楼县| 宜兰市| 荆州市| 晋中市| 赫章县| 大理市| 镇沅| 鞍山市| 马公市| 洞口县| 大姚县| 保德县| 商南县| 砀山县| 黑山县| 明光市| 崇义县| 盖州市| 苏尼特左旗| 南康市| 泾川县| 贵南县| 西昌市| 綦江县| 合川市| 东安县| 安西县| 南汇区| 武川县| 集贤县| 永州市| 长治市| 东乡| 蕲春县| 东明县| 阳谷县| 吉安县| 河曲县| 和平区|