Ads 468x60px

##EasyReadMore##

16 3月, 2018

[c 語言] 設定 socket timeout 時間

在 socket connect 那行程式前加上以下程式

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

  if (setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
              sizeof(timeout)) < 0)
      error("setsockopt failed\n");

  if (setsockopt (sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
              sizeof(timeout)) < 0)
      error("setsockopt failed\n");

timeout.tv_sec = 10; //10 代表 10 就會放棄連線

Refer:

linux - How to set socket timeout in C when making multiple connections? - Stack Overflow - https://goo.gl/MT6vFW

0 意見:

張貼留言

 
Blogger Templates