if (WSAStartup(0x202,&wsaData) == SOCKET_ERROR) { fprintf(stderr,"WSAStartup failed with error %d\n",WSAGetLastError()); WSACleanup(); return -1; } if (port == 0){ Usage(argv[0]); } // // Attempt to detect if we should call gethostbyname() or // gethostbyaddr() if (isalpha(server_name[0])) { /* server address is a name */ hp = gethostbyname(server_name); } else { /* Convert nnn.nnn address to a usable one */ addr = inet_addr(server_name); hp = gethostbyaddr((char *)&addr,4,AF_INET); } if (hp == NULL ) { fprintf(stderr,"Client: Cannot resolve address [%s]: Error %d\n", server_name,WSAGetLastError()); WSACleanup(); exit(1); }
能说详细点吗?举个例子?
谢谢
addr_in.sin_family = AF_INET;
addr_in.sin_port = htons(iPort);
USES_CONVERSION;
char* pszServer = W2A(bstrServer);
ULONG ulAddr = inet_addr(pszServer);
if( INADDR_NONE != ulAddr ) // ip
{
addr_in.sin_addr.s_addr = ulAddr;
}
else // 域名
{
hostent* pHost = gethostbyname( pszServer );
memcpy((char*)&addr_in.sin_addr,pHost->h_addr,pHost->h_length);
}
WSADATA wsaData;
char name[255];
CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
sprintf(name,"%s","http://www.sohu.com");
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{ if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}
WSACleanup( );
}
DWORD dwIP = INADDR_NONE;
PHOSTENT pHost = gethostbyname( (char *)pParam );
if(pHost == NULL)
return INADDR_NONE;
dwIP = inet_addr( inet_ntoa(*(IN_ADDR *)*pHost->h_addr_list) );
return dwIP;
}