2011년 9월 26일 월요일

WSAStartup() 함수로 winsock 초기화


윈도우에서 winsock 사용시 WSAStartup 함수로 winsock 을 초기화해야한다.


#if defined(__WIN32__) || defined(_WIN32)
#ifndef IMN_PIM
#define WS_VERSION_CHOICE1 0x202/*MAKEWORD(2,2)*/
#define WS_VERSION_CHOICE2 0x101/*MAKEWORD(1,1)*/
int initializeWinsockIfNecessary(void) {
/* We need to call an initialization routine before
* we can do anything with winsock.  (How fucking lame!):
*/
static int _haveInitializedWinsock = 0;
WSADATA wsadata;

if (!_haveInitializedWinsock) {
if ((WSAStartup(WS_VERSION_CHOICE1, &wsadata) != 0)
   && ((WSAStartup(WS_VERSION_CHOICE2, &wsadata)) != 0)) {
return 0; /* error in initialization */
}
    if ((wsadata.wVersion != WS_VERSION_CHOICE1)
       && (wsadata.wVersion != WS_VERSION_CHOICE2)) {
        WSACleanup();
return 0; /* desired Winsock version was not available */
}
_haveInitializedWinsock = 1;
}

return 1;
}
#else
int initializeWinsockIfNecessary(void) { return 1; }
#endif
#else
#define initializeWinsockIfNecessary() 1
#endif

...

// 윈속 초기화

  if (!initializeWinsockIfNecessary()) {
    socketErr(env, "Failed to initialize 'winsock': ");
    return -1;
  }


댓글 없음:

댓글 쓰기