Winsock实现主机名与IP地址之间的转换
#include <winsock2.h>
#include <stdio.h>
#include <conio.h>
#include <stdio.h>
#include <conio.h>
WSADATA wsaData;
bool InitSocket()
{
WORD wVersionRequested;
int err;
wVersionRequested = MAKEWORD( 2, 0 ); //询问Winsock 2.0版本
err = WSAStartup( wVersionRequested, &wsaData ); //加载初始化Windows Sockets DLL
if ( err != 0 ) {
printf("没有Windows Socket动态库!\n");
getch();
return false;
}
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 ) {
printf("需要Windows Socket 2!\n");
getch();
WSACleanup( );
return false;
}
return true;
}
{
WORD wVersionRequested;
int err;
wVersionRequested = MAKEWORD( 2, 0 ); //询问Winsock 2.0版本
err = WSAStartup( wVersionRequested, &wsaData ); //加载初始化Windows Sockets DLL
if ( err != 0 ) {
printf("没有Windows Socket动态库!\n");
getch();
return false;
}
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 ) {
printf("需要Windows Socket 2!\n");
getch();
WSACleanup( );
return false;
}
return true;
}
int main(int argc, char* argv[])
{
//local variable declaration
// WSADATA wsaData;
LPHOSTENT lpHostEnt;
DWORD dwIPAddr;
LPSTR szIPAddr;
char buf_DomName[24]=" ";
char buf_IP[15]=" ";
{
//local variable declaration
// WSADATA wsaData;
LPHOSTENT lpHostEnt;
DWORD dwIPAddr;
LPSTR szIPAddr;
char buf_DomName[24]=" ";
char buf_IP[15]=" ";
if(!InitSocket())
printf("WinSock start error!");
else
printf("program is running:\n");
printf("WinSock start error!");
else
printf("program is running:\n");
printf("DNS resolve test program's ready");
printf(" (no input equals local machine)........\n");
printf("\n");
printf(" (no input equals local machine)........\n");
printf("\n");
//start domain name resolve here.
printf("Please enter the domain name:");
gets(buf_DomName);
printf("Please enter the domain name:");
gets(buf_DomName);
//perform resolve here.
lpHostEnt = gethostbyname(buf_DomName);
if(!lpHostEnt)
{
//error occurs!
printf("Could not get IP address!");
printf("\n");
}
else
{
//convert & output
szIPAddr = inet_ntoa(*(LPIN_ADDR)*(lpHostEnt->h_addr_list));
printf(szIPAddr);
printf("\n");
}
lpHostEnt = gethostbyname(buf_DomName);
if(!lpHostEnt)
{
//error occurs!
printf("Could not get IP address!");
printf("\n");
}
else
{
//convert & output
szIPAddr = inet_ntoa(*(LPIN_ADDR)*(lpHostEnt->h_addr_list));
printf(szIPAddr);
printf("\n");
}
//start IP resolve here.
printf("Please enter the IP address:");
gets(buf_IP);
printf("Please enter the IP address:");
gets(buf_IP);
dwIPAddr = inet_addr(buf_IP);
if(dwIPAddr == INADDR_NONE)
//IP address validation failed!
printf("Invalid Internet address!\n");
else
{
lpHostEnt = gethostbyaddr((LPSTR)&dwIPAddr,sizeof(dwIPAddr),AF_INET);
if(!lpHostEnt)
{
//error occures!
printf("Could not get host name!");
printf("\n");
}
else
{
//output
printf(lpHostEnt->h_name);
printf("\n");
}
}
if(dwIPAddr == INADDR_NONE)
//IP address validation failed!
printf("Invalid Internet address!\n");
else
{
lpHostEnt = gethostbyaddr((LPSTR)&dwIPAddr,sizeof(dwIPAddr),AF_INET);
if(!lpHostEnt)
{
//error occures!
printf("Could not get host name!");
printf("\n");
}
else
{
//output
printf(lpHostEnt->h_name);
printf("\n");
}
}
//shut down the socket
WSACleanup();
printf("\n");
return 0;
}