nonblock IO (Linux, solaris)
글쓴이: 하하 / 작성시간: 월, 2003/11/17 - 5:28오후
#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가 안됩니다. - - ;;
왜 그런지.. 답변 주시면 감사합니다.. ^^.;;;;
Forums:
여기에 옮겨 적을 때 잘못 옮겨 적으신 건지 모르겠습니다만... set_
여기에 옮겨 적을 때 잘못 옮겨 적으신 건지 모르겠습니다만... set_nonblock 의 두번째 fcntl 의 마지막 인자가 좀 이상하죠? :-)
좋은 하루 되세요!
- - ;; 역시 집중안될때 코딩을 하지 말아야 겠단.. 생각이.
- - ;; 역시 집중안될때 코딩을 하지 말아야 겠단..
생각이.
“바람에게도 길은 있다. 나는 비로소 나의 길을 가느니. 길은 언제나 어디에나 있다.”
댓글 달기