nonblock IO (Linux, solaris)

하하의 이미지

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <stdio.h>


static void set_block(int sock);
static void set_nonblock(int sock);

int main()
{

        struct  sockaddr_in cli;
        int     fd;
        char    *chr;
        int     *num;

        int     cnt=5;
        int     r=0;
        fd_set  mask;


        struct timeval timeout;


        struct linger option;


        printf("START!!!\n");


        if ( (fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)  {
                        exit(1);
        }


        option.l_onoff  = 1;
        option.l_linger = 0;

        setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&option, sizeof(option));



        bzero((void*)&cli, sizeof(cli));
        cli.sin_family = AF_INET;
        cli.sin_addr.s_addr = inet_addr("211.39.143.147");
        cli.sin_port = htons((u_short)6060);

        num = (int *)malloc(sizeof(int));
        *num = 0x50525456;

        chr = (char *) num;


        set_nonblock(fd);


        if(connect(fd, (struct sockaddr*)&cli, sizeof(cli)) ==  0) {
                printf("connection OK!!!\n");
                goto L1;
        }



        printf(" errno %d , errmsg %s \n", errno, strerror(errno));



        if (errno != EINPROGRESS) {
                goto L0;
        }


        errno = 0;
        FD_ZERO(&mask);
        FD_SET(fd, &mask);
        timeout.tv_sec  = 2;
        timeout.tv_usec = 0;


        if ( (r = select(fd+1, NULL, &mask, NULL, &timeout)) == 0 ) {
                printf("[ERROR] %s \n", strerror(errno));
                goto L0;
        }

        printf(" return value ((%d)) \n", r);


        if ( r < 0 ) {
                printf("[ERROR] %s \n", strerror(errno));
                goto L0;
        }


L1:

        while(cnt--) {

                send(fd, chr, 4, 0);
                printf("%c.%c.%c.%c\n", chr[0], chr[1], chr[2], chr[3]);
                printf("%s\n", chr);

                sleep(1);

        }



L0:

        set_block(fd);

        close(fd);

        exit(0);
}


static void set_nonblock(int sock)
{
        int val;

        val = fcntl(sock, F_GETFL, 0);
        assert(val != -1);
        val |= O_NONBLOCK;
        val = fcntl(sock, F_SETFL, 0);
        assert(val != -1);

}

static void set_block(int sock)
{
        int val;

        val = fcntl(sock, F_GETFL, 0);
        assert(val != -1);
        val &= ~O_NONBLOCK;
        val = fcntl(sock, F_SETFL, val);
        assert(val != -1);

}

위 코드는 특정 IP,PORT에 데이터를 전송하는 간단한 클라이언트입니다.

제가 묻고 싶은건 nonblock IO에 관한 리눅스, 솔라리스의 차이점에 관한

것입니다.

첫째. 위 아이피 포트에 접근시 strerror(errno)의 메세지는 "No route to

host" 입니다.

둘째. 같은 소스를 돌려보면 리눅스는 nonblock fd로 인해 connect이 안되면

select까지 가야 하지만 route자체가 안잡혀 EINPROGRESS if문을 타고

바로 빠지고.. 솔라리스 같은 경우.. connect 함수에서 nonblock 이 아예 먹

질 않습니다.(connect에서 block 되어 계속 SYN_SEND를 보냅니다)

결론은.. 솔라리스가 nonblock IO가 안됩니다. - - ;;

왜 그런지.. 답변 주시면 감사합니다.. ^^.;;;;

raymundo의 이미지

여기에 옮겨 적을 때 잘못 옮겨 적으신 건지 모르겠습니다만... set_nonblock 의 두번째 fcntl 의 마지막 인자가 좀 이상하죠? :-)

좋은 하루 되세요!

하하의 이미지

- - ;; 역시 집중안될때 코딩을 하지 말아야 겠단..

생각이.

“바람에게도 길은 있다. 나는 비로소 나의 길을 가느니. 길은 언제나 어디에나 있다.”

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.