第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C:套接字連接超時

C:套接字連接超時

C
嗶嗶one 2019-10-29 11:02:35
我有一個簡單的程序來檢查端口是否打開,但是我想縮短套接字連接的超時時間,因為默認值太長了。我不確定如何執(zhí)行此操作。這是代碼:#include <sys/socket.h>#include <sys/time.h>#include <sys/types.h>#include <arpa/inet.h>#include <netinet/in.h>#include <errno.h>#include <fcntl.h>#include <stdio.h>#include <netdb.h>#include <stdlib.h>#include <string.h>#include <unistd.h>int main(int argc, char **argv) {    u_short port;                /* user specified port number */    char addr[1023];             /* will be a copy of the address entered by u */    struct sockaddr_in address;  /* the libc network address data structure */    short int sock = -1;         /* file descriptor for the network socket */    if (argc != 3) {        fprintf(stderr, "Usage %s <port_num> <address>", argv[0]);        return EXIT_FAILURE;    }    address.sin_addr.s_addr = inet_addr(argv[2]); /* assign the address */    address.sin_port = htons(atoi(argv[2]));            /* translate int2port num */    sock = socket(AF_INET, SOCK_STREAM, 0);    if (connect(sock,(struct sockaddr *)&address,sizeof(address)) == 0) {        printf("%i is open\n", port);    }      close(sock);    return 0;}
查看完整描述

3 回答

?
夢里花落0921

TA貢獻1772條經(jīng)驗 獲得超6個贊

關(guān)于使用select()/ 的答案poll()是正確的,應該以這種方式編寫代碼以便于移植。


但是,由于您使用的是Linux,因此可以執(zhí)行以下操作:


int synRetries = 2; // Send a total of 3 SYN packets => Timeout ~7s

setsockopt(fd, IPPROTO_TCP, TCP_SYNCNT, &synRetries, sizeof(synRetries));

請參閱man 7 tcp和man setsockopt。


我使用它來加快我需要快速修補的程序中的連接超時。不能通過select()/將其黑客破解為超時poll()。


查看完整回答
反對 回復 2019-10-29
  • 3 回答
  • 0 關(guān)注
  • 671 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號