C and C++ gets sample code for its own IP and domain name fragments

  • 2020-11-25 07:24:52
  • OfStack

Judge large end order and small end order:

Generally, there are two ways to store the value in memory: one is the large tail byte order, and the other is the small tail, such as 0x01020304. If the value is stored in the large tail way, the storage way is 01 02 03 04 And storing it in a small tail is 04 03 02 01 Generally, CPU compatible with Windows operating system is small tail mode, while CPU compatible with UNIX operating system is mostly large tail mode. The size of weft can be determined by using two methods.


#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")

//  Variable method judgment 
void GetA()
{
	DWORD dwSmallNum = 0x01020304;
	if (*(BYTE *)&dwSmallNum == 0x04)
		printf(" Small end byte order  \n");
	else
		printf(" Large end byte order  \n");
}

//  Direct conversion method 
void GetB()
{
	DWORD dwSmallNum = 0x01020304;
	if (dwSmallNum == htonl(dwSmallNum))
		printf(" Large end byte order  \n");
	else
		printf(" Small end byte order  \n");
}

int main(int argc, char *argv[])
{
	GetA();
	GetB();

	system("pause");
	return 0;
}

Obtain IP by domain name:

By using the winsocket library gethostbyname() You can extract the alias name, address type and other information corresponding to a domain name.


#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")

BOOL GetHostByName(char * HostName)
{
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
		return FALSE;

	struct hostent *ptr = gethostbyname(HostName);
	if (!ptr){ return FALSE; }

	printf(" Address type : %s \n", (ptr->h_addrtype == AF_INET) ? "IPV4" : "IPV6");

	for (int i = 0; ptr->h_aliases[i]; i++){
		printf(" The alias  [%d]: %s \n", i + 1, ptr->h_aliases[i]);
	}

	for (int i = 0; ptr->h_addr_list[i]; i++){
		printf("IP address  [%d]: %s \n", i + 1, inet_ntoa(*(struct in_addr*)ptr->h_addr_list[i]));
	}
	WSACleanup();
	return TRUE;
}

int main(int argc ,char *argv[])
{
	GetHostByName("www.baidu.com");
	system("pause");
	return 0;
}

Name of host IP address:

Sometimes we need to get our own IP address, and Here I've encapsulated two ways to get the IP address.


#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")

char * GetLocalHostName()
{
	WSADATA wsaData;
	HOSTENT *pHost;
	char szHostName[256];

	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
		exit(0);

	if (gethostname(szHostName, 256) == 0)
	{
		char *Host;
		Host = (char *)malloc(1024);
		pHost = gethostbyname(szHostName);
		strcpy(Host, pHost->h_name);
		return Host;
	}
	return "";
}

char * GetLocalHostAddr(int Count)
{
	WSADATA wsaData;
	HOSTENT *pHost;
	char szHostName[256];

	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
		exit(0);

	if (gethostname(szHostName, 256) == 0)
	{
		char tmp[15];
		char *Addr;
		pHost = gethostbyname(szHostName);
		int index = 0;

		for (; index < 10; index++)
		{
			if (pHost->h_addr_list[index] == NULL)
				break;
		}

		sprintf(tmp, "%d.%d.%d.%d",
			pHost->h_addr_list[0][0] & 0xff,
			pHost->h_addr_list[0][1] & 0xff,
			pHost->h_addr_list[0][2] & 0xff,
			pHost->h_addr_list[0][3] & 0xff);

		Addr = (char *)malloc(15);
		strcpy(Addr, tmp);
		return Addr;
	}
	return "";
}

int main(int argc, char *argv[])
{
	char *hostname = GetLocalHostName();
	printf(" The machine name : %s \n", hostname);

	char *hostaddr = GetLocalHostAddr(0);
	printf(" This machine IP: %s \n", hostaddr);

	system("pause");
	return 0;
}

Above is C/C++ to obtain their own IP and domain fragment sample code details, more about C/C++ to obtain their own IP and domain fragment information please pay attention to other related articles on this site!


Related articles: