The gethostbyname function resolves the string returned by a successful call to gethostname.
gethostname
The Windows Sockets gethostname function returns the standard host name for the local machine.int gethostname(
  char FAR *name,  
  int namelen       
);
Parameters
name 
[out] Pointer to a buffer that receives the local host name. 
namelen (*****************注意这里*****************)
[in] Length of the buffer. 

解决方案 »

  1.   

    初始化了吗?先调用WSAStartup(函数名记不清了)
      

  2.   

    就是,先要初始化。如果你已经做了,那么就是你用的不对
    我上面贴了,使用的方法。
    如果你想对http://www.csdn.net/expert/topic/771/771757.xml?temp=.7499658这样的url获得ip,恐怕不行,除非是www.csdn.net这样的
      

  3.   

    struct hostent FAR *gethostbyname(
      const char FAR *name//使用gethostname第1个参数返回的内容作为参数
    );
    int gethostname(
      char FAR *name,  
      int namelen       
    );
    Parameters
    name 
    [out] Pointer to a buffer that receives the local host name. 
    namelen (*****************注意这里*****************)
    [in] Length of the buffer.
      

  4.   

    gethostname返回也为空,是否需要WSAStartup初始化?怎样做?
      

  5.   

    WSAData data;
    if(WSAStartup(MAKEWORD(2,2),&data)!=0)
    {
         //initial failed!
         return;
    }
    ...
    ...
      

  6.   

    谢谢,但是我的程序跟踪运行到gethostbyname就要很长时间,最后返回还是为空,真的很奇怪
      

  7.   

    用WSAGetLastError()取的错误代码
    用FormatMessage()讲错误代码转换成语言表述的错误类型
    然后察看这个错误类型代表的意思
      

  8.   

    WSAStartup();
    gethostname(......);
    WSAClean();
      

  9.   

    你的计算机应当设定为自动分配IP,gethostbyname在找不到计算机的情况下,确实需要很多时间.