소켓 디스크립터를 Blocking->Non-Blocking으로 변경

#include <fcntl.h>

// where socketfd is the socket you want to make non-blocking
int status = fcntl(socketfd, F_SETFL, fcntl(socketfd, F_GETFL, 0) | O_NONBLOCK);

if (status == -1){
  perror("calling fcntl");
  // handle the error.  By the way, I've never seen fcntl fail in this way
}

혹은 소켓 디스크립터 선언시 매개변수에 SOCK_NONBLOCK을 지정한다.
// client side  
 int socketfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);    

// server side - see man page for accept4 under linux    
int socketfd = accept4( ... , SOCK_NONBLOCK);


Source : http://stackoverflow.com/questions/1543466/how-do-i-change-a-tcp-socket-to-be-non-blocking

댓글