类库用是Ws2_32.lib 
但是这个函数在有的机器可以,有的又不行,请问什么原因?

解决方案 »

  1.   

    //you maybe add this code first
    #pragma comment(lib,"ws2_32")
    and before you use this function you should init socket,there is my some code:
    int CMyDialog::GetIPAddress(const CString &sHostName, CString &sIPAddress)
    {
    WORD wVersionRequested;
    WSADATA wsaData;
    int err; 
    wVersionRequested = MAKEWORD( 2, 2 );
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 )
    {
    /* Tell the user that we could not find a usable */
    /* WinSock DLL.                                  */
    return 0;
    }
    ...
    }
              
      

  2.   

    将类库用wsock32.lib原因:ws2_32.lib是winsock 2的库,wsock32.lib是winsock 1.1的库,win9x的机器可能还没有升级到winsock 2,所以你用ws2_32.lib编译的程序不能访问winsock2的dll,所以不行。