2011년 9월 26일 월요일

windows 에서 winsock select 함수사용


winsock select 함수 사용시 fd_set 이 비어있으면 select 함수에서 에러발생
이때 WSAGetLastError() 함수로 에러값을 보고 dummy 소켓을 생성해서 fd_set에
넣어줘야함. 아래 소스 참조


  fd_set readSet = fReadSet; // make a copy for this select() call

  struct timeval tv_timeToDelay;
  tv_timeToDelay.tv_sec = 1;
  tv_timeToDelay.tv_usec = 0;


  int selectResult = select(fMaxNumSockets, &readSet, NULL, NULL,
   &tv_timeToDelay);
  if (selectResult < 0) {
#if defined(__WIN32__) || defined(_WIN32)
    int err = WSAGetLastError();
    // For some unknown reason, select() in Windoze sometimes fails with WSAEINVAL if
    // it was called with no entries set in "readSet".  If this happens, ignore it:
    if (err == WSAEINVAL && readSet.fd_count == 0) {
      err = 0;
      // To stop this from happening again, create a dummy readable socket:
      int dummySocketNum = socket(AF_INET, SOCK_DGRAM, 0);
      FD_SET((unsigned)dummySocketNum, &fReadSet);
    }
    if (err != 0) {
#else
    if (errno != EINTR && errno != EAGAIN) {
#endif
// Unexpected error - treat this as fatal:
#if !defined(_WIN32_WCE)
perror("BasicTaskScheduler::SingleStep(): select() fails");
#endif
// exit(0);
      }
  }

댓글 없음:

댓글 쓰기