Visual Studio C++ 2008
我正在使用这个代码。但是,gethostbyname总是返回错误。在我看来一切都很好,所以我不明白为什么我会得到这个错误。
这是我用来获取gethostbyname的代码。
你觉得我可能做错了什么吗?
int32_t sockfd;
/* struct definition */
struct sockaddr_in conn_addr;
/* gethostbyname for the function and struct definition */
struct hostent *server_hostname;
/* set address to connect to the local loopback */
char buffer[BUF_SIZE] = "127.0.0.1";
char data[BUF_SIZE] = {0};
/* getting hostname for the ip address stored in the buffer */
if((server_hostname = gethostbyname(buffer)) == NULL)
{
/* gethostbyname uses a special h_errno for error number */
fprintf(stderr, "gethostbyname [ %s ] [ %s ] [ %d ]\n", strerror(h_errno), __FUNCTION__, __LINE__);
return CS_FAILURE;
}返回的错误是“未知错误”,这不会有太大帮助。
非常感谢您的建议,
发布于 2010-03-07 17:24:40
需要添加WSAStartup;
WSADATA wsaData;
struct hostent *remoteHost;
char *host_name = "127.0.0.1";
WSAStartup(MAKEWORD(2, 2), &wsaData);
remoteHost = gethostbyname(host_name);发布于 2010-03-07 17:24:11
你应该能够使用WSAGetLastError得到“真正的”错误。
顺便说一句,我假设你在调用gethostbyname之前已经调用了WSAStartup来初始化套接字子系统?
https://stackoverflow.com/questions/2395732
复制相似问题