C# 如何将域名地址转换成IP

来源:百度知道 编辑:UC知道 时间:2024/05/21 12:07:32
如题

Dns.GetHostAddresses(域名)
得到的是一个ip数组。
IPAdress[] ips=Dns.GetHostAddresses(域名);
一个的话取第一项即可。

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;
}
struct hostent *lpHostEnt = gethostbyname (sHostName);
if (lpHostEnt == NULL)
{
// An error occurred.
sIPAddress = _T("");
return WSAGetLastError();
}

LPSTR lpAddr = lpHos