今天做的实验,现将代码分享如下
1.Windows socket DLL加载
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
注意,在调试的时候要将SDL检查设置为“否”,因为inet_ntoa()函数较为古老,之所以使用它是因为非常简单,适合新手
2.初始化
if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0)//初始化
{
return 1;
}
3.获取主机IP地址(完整代码)
#include<iostream>
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
WSADATA WSAData;
hostent* remoteHost;
char hostName[255];
char* IP;
in_addr addr;
int i = 0;
if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0)//初始化
{
return 1;
}
if (gethostname(hostName, sizeof(hostName)) != 0)
{
cout << "本地主机错误";
return NULL;
}
remoteHost = gethostbyname(hostName);
if (remoteHost == NULL)
{
return -1;
}
cout << "主机名称:" << remoteHost->h_name<<endl;
while (remoteHost->h_addr_list[i] != 0)
{
addr.s_addr = *(u_long*)remoteHost->h_addr_list[i++];
cout<< "\nIP地址:"<<inet_ntoa(addr)<<endl;
}
WSACleanup();
}
4.获取指定域名的IP地址
#include<iostream>
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
WSADATA WSAData;
hostent* pHost;
in_addr addr;
char* addIP;
char hostIP[255];
if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0)//初始化
{
return 1;
}
cout << "请输入要查询的域名:";
cin >> hostIP;
pHost = gethostbyname(hostIP);
if (pHost ==NULL)
{
return -1;
}
for (int i = 0; i < 4; i++)
{
char* p = pHost->h_addr_list[i];
if (p == NULL)
{
break;
}
memcpy(&addr.S_un.S_addr, p, pHost->h_length);
addIP = inet_ntoa(addr);//转换IP地址格式
cout << "\n域名:" << hostIP;
cout << "\nIP地址:" << addIP;
cout << "\n服务器名字:" << pHost->h_name<<endl;
}
WSACleanup();
}
最后放上老师给的代码,在使用的时候要注意在项目-属性-调试-命令参数里面输入目标域名
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
int main(int argc, char** argv)
{
// 声明和初始化变量
WSADATA wsaData;
int iResult;
DWORD dwError;
int i = 0;
struct hostent* remoteHost;
char* host_name;
struct in_addr addr;
char** pAlias;
// 验证参数的合法性
if (argc != 2) {
printf("usage: GetHostIP hostname\n");
return 1;
}
// 初始化Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
host_name = argv[1];
printf("Calling gethostbyname with %s\n", host_name);
remoteHost = gethostbyname(host_name);
// 对返回结果进行判断
if (remoteHost == NULL)
{
dwError = WSAGetLastError();
if (dwError != 0) {
if (dwError == WSAHOST_NOT_FOUND) {
printf("Host not found\n");
return 1;
}
else if (dwError == WSANO_DATA) {
printf("No data record found\n");
return 1;
}
else {
printf("Function failed with error: %ld\n", dwError);
return 1;
}
}
}
else
{
// 输出地址类型和地址长度
printf("Function returned:\n");
printf("\tOfficial name: %s\n", remoteHost->h_name);
for (pAlias = remoteHost->h_aliases; *pAlias != 0; pAlias++) {
printf("\tAlternate name #%d: %s\n", ++i, *pAlias);
}
printf("\tAddress type: ");
switch (remoteHost->h_addrtype) {
case AF_INET:
printf("AF_INET\n");
break;
case AF_NETBIOS:
printf("AF_NETBIOS\n");
break;
default:
printf(" %d\n", remoteHost->h_addrtype);
break;
}
printf("\tAddress length: %d\n", remoteHost->h_length);
// 如果返回的是IPv4 的地址,则输出
i = 0;
if (remoteHost->h_addrtype == AF_INET){
while (remoteHost->h_addr_list[i] != 0) {
addr.s_addr = *(u_long*)remoteHost->h_addr_list[i++];
printf("\tIP Address #%d: %s\n", i, inet_ntoa(addr));
}
}
else if (remoteHost->h_addrtype == AF_NETBIOS){
printf("NETBIOS address was returned\n");
}
}
return 0;
}