char  caHostName[128]; 
int h;
CString str;
char FAR * hello;
struct hostent FAR * pHost;
int  namelen=18;
gethostname(caHostName,namelen);
pHost = gethostbyname(caHostName);//获得主机结构 
/* hello = inet_ntoa(*((in_addr *)pHost->h_addr));
*/ if  ((h=gethostname(caHostName,namelen))  ==  NULL) 

AfxMessageBox("open haha error!");

str.Format("int:%d",h); 
AfxMessageBox(str);
为什么运行返回-1啊?

解决方案 »

  1.   

    Return Values
    If no error occurs, this function returns zero. If an error occurs, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError. The following table shows the possible error codes.#define SOCKET_ERROR            (-1)
      

  2.   

    你调用WSAStartup初始化你的WS2_32.DIL库了吗,你程序的错误很可能是这个错误引起的