在linux下玩轉(zhuǎn)帶有超時(shí)時(shí)間的connect函數(shù)
在之前的文章中,我們?cè)赪indows下玩過帶有超時(shí)時(shí)間的,本文我們?cè)趌inux下來玩。在某次面試中,還被遇到了這個(gè)問題,有意思。
直接上客戶端代碼:
#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)寫過多次,本文就不寫了。
經(jīng)測試,上述程序OK, 用tcpdump抓包,還能學(xué)到不少東西,比如SYN包重傳,RST包等。有點(diǎn)意思。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- 詳解linux下fsevents模塊引起的npm ls報(bào)錯(cuò)解決辦法
- linux下搭建go環(huán)境的安裝配置講解
- linux的cut命令用法總結(jié)
- 詳解linux系統(tǒng)輸入輸出管理和vim的常用功能
- linux shell之通過標(biāo)識(shí)測試文件系統(tǒng)屬性的方法示例
- linux shell中if的各種判斷
- linux shell之pushd、popd和dirs的使用講解
- linux shell之控制臺(tái)打印各種顏色字體和背景的實(shí)現(xiàn)方法
- linux下查看so或可執(zhí)行程序的依賴庫
- linux中alarm函數(shù)的實(shí)例講解
相關(guān)文章
Linux基礎(chǔ)學(xué)習(xí)之利用tcpdump抓包實(shí)例代碼
tcpdump是Linux下面的一個(gè)開源的抓包工具,和Windows下面的wireshark抓包工具一樣, 支持抓取指定網(wǎng)口、指定目的地址、指定源地址、指定端口、指定協(xié)議的數(shù)據(jù)。下面這篇文章主要給大家介紹了關(guān)于Linux基礎(chǔ)學(xué)習(xí)之利用tcpdump抓包的相關(guān)資料,需要的朋友可以參考下。2017-12-12ubuntu16.04下安裝openssh-server報(bào)依賴錯(cuò)誤的完美解決方法(非常不錯(cuò))
這篇文章主要介紹了ubuntu16.04下安裝openssh-server報(bào)依賴錯(cuò)誤的完美解決方法(非常不錯(cuò))的相關(guān)資料,需要的朋友可以參考下2016-11-11centos7云主機(jī)系統(tǒng)下掛載磁盤的方法
本篇文章主要介紹了centos7云主機(jī)系統(tǒng)下掛載磁盤的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02CentOS設(shè)置精準(zhǔn)時(shí)間的方法
下面小編就為大家分享一篇CentOS設(shè)置精準(zhǔn)時(shí)間的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02ubuntu18.0.4安裝mysql并解決ERROR 1698 (28000): Access denied for
這篇文章主要介紹了ubuntu18.0.4安裝mysql并解決ERROR 1698 (28000): Access denied for user 'root'@'localhost',現(xiàn)在將ubuntu18.0.4上安裝mysql并將碰到的問題記錄下來,感興趣的朋友一起看看吧2019-11-11Linux系統(tǒng)下netstat命令詳細(xì)介紹
大家好,本篇文章主要講的是Linux系統(tǒng)下netstat命令詳細(xì)介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Linux文件服務(wù)器實(shí)戰(zhàn)詳解(匿名用戶)
這篇文章主要介紹了Linux文件服務(wù)器實(shí)戰(zhàn)(匿名用戶),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06