网络编程(一) 获取主机以及指定域名的IP地址

本文介绍了如何使用Windows Socket (Winsock) 库在C++中实现获取本地主机和指定域名的IP地址。通过WSAStartup初始化,调用gethostbyname函数,结合inet_ntoa函数,可以轻松获取IP地址。代码示例详细展示了从主机名到IP地址转换的过程。
摘要由CSDN通过智能技术生成

今天做的实验,现将代码分享如下

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;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值