The first practice of network programming -- from debugging Dev C + + to querying host name and ip address

This is my first network programming practice, which was carried out at home during the outbreak. Compared with in school, the disadvantage of at home is that there is a problem and we can't ask for help from teachers and students in detail. With the help of students, with the help of network query, we successfully completed the experiment. I think someone might have the same difficulties as me, so I'll go through every step from debugging Dev C + + to writing.

Debug DEV C++

At first, I wanted to use VS, but I didn't touch vs before, and there were all kinds of strange and strange errors in the operation, so I changed to use DEV C + +, this software can also realize network programming, and its volume is smaller. Now I will introduce the debugging methods as follows.
First, create a New project, click "File", click "New" in the pull-down menu, and click "project"
Select the console application option

Right click the project on the left to open "project options"
In the Parameters column, open Add in the lower right corner

Find libws2? 32. A in the file directory and add it
The detailed catalog is:
Dev CPP -- MinGW32 -- lib, the Lib folder is a variety of dynamic libraries

After adding, open compiler options in Tools in the main menu, add "- lwsock32" in the General tab, and click OK to complete the compilation

Programming process

Initialize DLL

At the beginning of the experiment, I was in a state of stupidity, and I didn't know where to start or how to do it. After watching the course video again, I got a general idea.
I've learned a (false) truth - programming is never done, initializing the linkbase first
Initializing a DLL is before calling a socket function, so there is no problem initializing it first. Due to the complexity of the network, it is necessary to pay attention to the return value of the function at all times. If an error value is returned, it is necessary to exit the program in time with corresponding warning or prompt

WSAData data;
	if (WSAStartup(MAKEWORD(1, 1), &data) != 0)//Initialize the dynamic link library of winsocket, and check whether there is startup error 
	{
		cout << "Initialization error endl";
		return 0;//In case of any error, return the result directly and exit the program  
	}
Get host name

To get the hostname, we need to use the gethostname function. Let's first look at the function call value and return value. From Baidu Encyclopedia

This function is relatively simple. We only need to define a char array, and then put the array name and length into it to call

char zhuji[255];
	if (gethostname(zhuji, sizeof(zhuji)) == SOCKET_ERROR)//Use gethostname to query the host name. If it is correct, it will return 0. If it is wrong, it will return socket "error" 
	{
		cout << "Unable to get host name" << endl;
		return 0;
	}
	else
	{
		cout << "The name of the machine is:" << zhuji << endl;
	}

This returns the hostname

Query host ip address

gethostbyname function to query ip address

The return value of gethostbyname is a pointer to the structure host, and the parameter only needs the name of a char array. Therefore, before calling this function, we need to define a pointer to the host structure.
At the same time, it should be noted that the ip address is stored in the H ﹣ addr ﹣ list. This variable stores multiple ip addresses. We need to use the for loop to output them one by one.
In order to be beautiful and easy to watch, we use INET? Ntoa function to convert it into dotted decimal IP address

struct hostent *p = gethostbyname(zhuji);//The host is a data structure that defines a pointer to the data structure and assigns the function return value to the pointer 
	if (p == 0)//gethostbyname is used to query all ip addresses in the host 
	{
		cout << "Host name and could not be queried IP" << endl;
		return 0;
	}
	else
	{
		//Native IP: output all IPS of the native by using the cycle  
		for (int i = 0; p->h_addr_list[i] != 0; i++)//H ﹣ addr ﹣ list is a pointer to store ip address, through which ip value can be called out one by one 
		{
			struct in_addr in;//In ﹐ addr can represent a 32-bit ipv4 address 
			memcpy(&in, p->h_addr_list[i], sizeof(struct in_addr));//Copy function, copy the address pointed to by H ﹣ addr ﹣ list to the value of in 
			cout << "The first" << i + 1 << "Block NIC IP by:" << inet_ntoa(in) << endl;//Using INET? Ntoa() function to convert to dotted decimal ip address 
		}

	}
Query ip corresponding to domain name

This is relatively simple. Just change the above code, and use the gethostbyname function as well.

char yuming[255];
	cout<<endl<<"Please enter the domain name to query:";
	cin>>yuming; 
	struct hostent *q=gethostbyname(yuming);//Same as above, check ip according to domain name 
	if(!q)
	{
		cout<<"Domain name error!"<<endl;
		return 0; 
	}
	else
	{
		for(int i=0;q->h_addr_list[i];i++)//Just change the code a little bit 
		{
			struct in_addr inad;
			memcpy(&inad,q->h_addr_list[i],sizeof(struct in_addr));
			cout<<"IP The address is:"<<i+1<<inet_ntoa(inad)<<endl; 
		}
	}
Finally, don't forget to close the DLL
Whole code
#include <iostream>
#pragma comment(lib, "WS2_32")
#include <iostream>
#include <Winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include<bits/stdc++.h>//I'm used to adding universal header files for the sake of insurance 
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) 
{
	WSAData data;
	if (WSAStartup(MAKEWORD(1, 1), &data) != 0)//Initialize the dynamic link library of winsocket, and check whether there is startup error 
	{
		cout << "Initialization error endl";
		return 0;//Once there is an error, return the result directly and exit the program  
	}

	char zhuji[255];
	if (gethostname(zhuji, sizeof(zhuji)) == SOCKET_ERROR)//Use gethostname to query the host name. If it is correct, it will return 0. If it is wrong, it will return socket "error" 
	{
		cout << "Unable to get host name" << endl;
		return 0;
	}
	else
	{
		cout << "The name of the machine is:" << zhuji << endl;
	}

	struct hostent *p = gethostbyname(zhuji);//The host is a data structure that defines a pointer to the data structure and assigns the function return value to the pointer 
	if (p == 0)//gethostbyname is used to query all ip addresses in the host 
	{
		cout << "Host name and could not be queried IP" << endl;
		return 0;
	}
	else
	{
		//Native IP: output all IPS of the native by using the cycle  
		for (int i = 0; p->h_addr_list[i] != 0; i++)//H ﹣ addr ﹣ list is a pointer to store ip address, through which ip value can be called out one by one 
		{
			struct in_addr in;//In ﹐ addr can represent a 32-bit ipv4 address 
			memcpy(&in, p->h_addr_list[i], sizeof(struct in_addr));//Copy function, copy the address pointed to by H ﹣ addr ﹣ list to the value of in 
			cout << "The first" << i + 1 << "Block NIC IP by:" << inet_ntoa(in) << endl;//Using INET? Ntoa() function to convert to dotted decimal ip address 
		}

	}
	char yuming[255];
	cout<<endl<<"Please enter the domain name to query:";
	cin>>yuming; 
	struct hostent *q=gethostbyname(yuming);//Same as above, check ip according to domain name 
	if(!q)
	{
		cout<<"Domain name error!"<<endl;
		return 0; 
	}
	else
	{
		for(int i=0;q->h_addr_list[i];i++)//Just change the code a little bit 
		{
			struct in_addr inad;
			memcpy(&inad,q->h_addr_list[i],sizeof(struct in_addr));
			cout<<"IP The address is:"<<i+1<<inet_ntoa(inad)<<endl; 
		}
	}
	WSACleanup();//Finally, don't forget to close the dynamic library 
	return 0;
}
18 original articles published, praised and 599 visitors visited
Private letter follow

Tags: network Programming socket

Posted on Tue, 03 Mar 2020 19:46:21 -0800 by Atomiku