socket통신 connect()함수 리턴값에 대한 질문입니다.

cpulpul의 이미지

connect()함수의 리턴값에 대한 질문을 올립니다.

아래와 같이 nonblocking connect()함수가 있습니다.

최초 서버에 nonblocking connecting을 시도 합니다.

접속완료 되면 서버로 데이터를 전송합니다.

서버와 통신중 사용자가 임의로 연결을 종료하면 sockfd close한후

프로세서는 서버로 데이터를 전송하지 않고 내부에 데이터를 저장합니다.

다시 사용자가 임의로 서버로의 연결을 시도하면

아래의 함수로 nonblocking connecting을 시도중

이건 소스코드입니다.

printf("%d\n", errno);  // <<==== 여기가 9번으로 찍힙니다.
        if (errno != EINPROGRESS)
            return -1;
        perror("EINPROGRESS");

아래는 truss 결과입니다

/2:     so_socket(PF_INET, SOCK_STREAM, IPPROTO_IP, "", 1) = 4
/2:     fcntl(4, F_GETFL, 0x00000000)                   = 2
/2:     fcntl(4, F_SETFL, 0x00000082)                   = 0
/* 여기서 150번 에러 발생 */
/2:     connect(4, 0xDD8ADCC4, 16, 1)      Err#150 EINPROGRESS
/* 여기서  9번 에러 출력 */
/2:     write(1, " 9\n", 2)                             = 2
/2:     mmap(0x00000000, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0) = 0xDD7A0000
/2:     fcntl(4, F_GETFL, 0x00000000)                   = 130
/2:     fcntl(4, F_SETFL, 0x00000082)                   = 0
/2:     connect(4, 0xDD8ADCC4, 16, 1)                   Err#133 EISCONN
/2:     write(1, " 9\n", 2)                             = 2

이와 같은 현상이 발생합니다.

참고로 서버는 리눅스에서 컴파일한 바이너리 파일이고

클라이언트1은 리눅스에서 컴파일한 바이너리인데 위와 같은 문제가
발생하지 않고 정상동작 하고 있으며

클라이언트2는 솔라리스에서 컴파일한 바이너리이며 위와 같은 문제가
발생합니다.

non_blocking 연결 함수는 아래와 같습니다.

고수님들의 많은 조언부탁 드립니다.

int send_sockfd // 전역변수
int  conn_nonb(struct sockaddr_in * psendaddr, socklen_t slen)
{
    int  status, error;
    socklen_t len;
    fd_set rset, wset;
    struct timeval tval;


    int flags;
    flags = fcntl(send_sockfd, F_GETFL, 0);
    fcntl(send_sockfd, F_SETFL, flags | O_NONBLOCK);

    error = 0;
    status = connect(send_sockfd, (struct sockaddr *)psendaddr,
                   sizeof(*psendaddr));

    if (status < 0)
    {
printf("%d\n", errno);
        if (errno != EINPROGRESS)
            return -1;
        perror("EINPROGRESS");
    }

    // 연결성공
    if (status == 0)
        goto done;

    FD_ZERO(&rset);
    FD_SET(send_sockfd, &rset);
    wset = rset;
    tval.tv_sec = 5;
    tval.tv_usec = 0;

    // 5초간 대기
    if ((status = select(send_sockfd+1, &rset, &wset, NULL, &tval)) == 0)
    {
        errno = ETIMEDOUT;
        return -1;
    }
    if (FD_ISSET(send_sockfd, &rset) || FD_ISSET(send_sockfd, &wset))
    {
       len = sizeof(int);

       if (getsockopt(send_sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
       {
           return -1;
        }
    }

done:
    fcntl(send_sockfd, F_SETFL, flags);

    if (error)
    {
        errno = error;
        return -1;
    }
    return 0;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.