newtype
::: newtype의 지식창고 :::
newtype
전체 방문자
오늘
어제
  • 분류 전체보기 (392)
    • Dev (214)
      • C, C++ (43)
      • Go (5)
      • Web (49)
      • DBMS (21)
      • DevOps (8)
      • Java (2)
      • Windows, Win32 (4)
      • Visual Basic (5)
      • C# (2)
      • Mobile (25)
      • SQL CE (7)
      • Google Map (6)
      • Python (2)
      • cygwin (2)
      • 기타 (32)
      • Ruby (1)
    • 명언 (10)
    • 모임 (18)
      • 붕주회 (3)
      • 신흥컴정 (14)
      • 웹20기 (1)
    • 사진 (8)
    • 불펌 (29)
    • 막글 (98)
    • 게임 (6)
    • 여행 (8)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

  • whoami
05-11 10:21
hELLO · Designed By 정상우.
newtype

::: newtype의 지식창고 :::

Dev/C, C++

socket connect timeout

2007. 5. 29. 17:34

connection 할때 timeout 처리에 관해 찾아 보는 중.
nonblocking socket을 이용한 Rechard stevens의 소스가 있더군요.

#include "unp.h"

int
connect_nonb(int sockfd, const SA *saptr, socklen_t salen, int nsec)
{
int flags, n, error;
socklen_t len;
fd_set rset, wset;
struct timeval tval;

flags = Fcntl(sockfd, F_GETFL, 0);
Fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);

error = 0;
if ( (n = connect(sockfd, (struct sockaddr *) saptr, salen)) < 0)
if (errno != EINPROGRESS)
return(-1);

/* Do whatever we want while the connect is taking place. */

if (n == 0)
goto done; /* connect completed immediately */

FD_ZERO(&rset);
FD_SET(sockfd, &rset);
wset = rset;
tval.tv_sec = nsec;
tval.tv_usec = 0;

if ( (n = Select(sockfd+1, &rset, &wset, NULL,
nsec ? &tval : NULL)) == 0) {
close(sockfd); /* timeout */
errno = ETIMEDOUT;
return(-1);
}

if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
len = sizeof(error);
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
return(-1); /* Solaris pending error */
} else
err_quit("select error: sockfd not set");

done:
Fcntl(sockfd, F_SETFL, flags); /* restore file status flags */

if (error) {
close(sockfd); /* just in case */
errno = error;
return(-1);
}
return(0);
}



예전에 포스팅 한 http socket client 에 적용하면서 windows 버전으로도 만들어 봤습니다.

int CHttpSocket::connect_nonb(int sockfd, const struct sockaddr *saptr, socklen_t salen, struct timeval tval)
{
    int                flags, n, error;
    socklen_t        len;
    fd_set            rset, wset;

#ifdef WIN32
    int nonblocking =1;
    ioctlsocket(sockfd, FIONBIO, (unsigned long*) &nonblocking);
#else
    flags = fcntl(sockfd, F_GETFL, 0);
    fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
#endif

    error = 0;
    if ( (n = connect(sockfd, (struct sockaddr *) saptr, salen)) < 0)
    {
#ifdef WIN32
        errno = WSAGetLastError();
#endif
        if (errno != EINPROGRESS && errno!= EWOULDBLOCK)
        {
            return(-1);
        }
    }

    /* Do whatever we want while the connect is taking place. */

    if (n == 0)
        goto done;    /* connect completed immediately */

    FD_ZERO(&rset);
    FD_SET(sockfd, &rset);
    wset = rset;

    if ( (n = select(sockfd+1, &rset, &wset, NULL,
                     ((tval.tv_sec>0) || (tval.tv_usec>0))? &tval : NULL)) == 0)
    {
        close(sockfd);        /* timeout */
        errno = ETIMEDOUT;
        return(-1);
    }

    if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset))
    {
#ifndef WIN32
        len = sizeof(error);
        if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
            return(-1);            /* Solaris pending error */
#endif
    } else
        return(-1); //err_quit("select error: sockfd not set");

done:
#ifdef WIN32
    nonblocking =0;
    ioctlsocket(sockfd, FIONBIO, (unsigned long*) &nonblocking);
#else
    fcntl(sockfd, F_SETFL, flags);    /* restore file status flags */
#endif

    if (error) {
        close(sockfd);        /* just in case */
        errno = error;
        return(-1);
    }
    return(0);
}


반응형

'Dev > C, C++' 카테고리의 다른 글

trim  (4) 2007.09.14
xor을 이용한 swap 함수  (0) 2007.08.09
URL Encoding/Decoding 함수  (1) 2007.04.05
Registry 읽고 쓰기 재활용  (0) 2007.03.31
애플리케이션 개발시의 메모리 디버깅 : 메모리 누수 발견 기법  (0) 2007.03.19
    'Dev/C, C++' 카테고리의 다른 글
    • trim
    • xor을 이용한 swap 함수
    • URL Encoding/Decoding 함수
    • Registry 읽고 쓰기 재활용
    newtype
    newtype
    지극히 개인적인 지식의 창고

    티스토리툴바