Home > Back-end >  For a great god!!!!! Through the IP access to equipment name within a local area network (LAN) funct
For a great god!!!!! Through the IP access to equipment name within a local area network (LAN) funct

Time:10-30

Rescue a great god!!!!!!!!!! Through the IP access to equipment name within a local area network (LAN) function gethostbyaddr () returns all bogon, what to do!!

CodePudding user response:

 
#include
#include
#include

//link with ws2_32. Lib
# pragma comment (lib, "Ws2_32. Lib")

Int __cdecl main (int arg c, char * * argv)
{

//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Declare and initialize variables
WSADATA WSADATA=https://bbs.csdn.net/topics/{0};
Int iResult=0;

DWORD dwRetval;

Struct sockaddr_in saGNI;
Char hostname [NI_MAXHOST];
Char servInfo [NI_MAXSERV];
U_short port=27015;

//Validate the parameters
If (arg c!=2) {
Printf (" IPv4 address usage: % s \ n ", argv [0]).
Printf (" to return the hostname \ n ");
127.0.0.1 printf (" % s \ n ", argv [0]).
return 1;
}
//Initialize Winsock
IResult=WSAStartup (MAKEWORD (2, 2), & amp; WsaData);
If (iResult!=0) {
Printf (" WSAStartup failed: % d \ n ", iResult);
return 1;
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Set up sockaddr_in structure which is passed
//to the getnameinfo function
SaGNI. Sin_family=AF_INET;
SaGNI. Sin_addr. S_addr=inet_addr (argv [1]).
SaGNI. Sin_port=htons (port);

//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Call getnameinfo
DwRetval=getnameinfo ((struct sockaddr *) & amp; SaGNI,
Sizeof (struct sockaddr),
The hostname,
NI_MAXHOST servInfo, NI_MAXSERV NI_NUMERICSERV);

If (dwRetval!=0) {
Printf (" getnameinfo failed with the error # % ld \ n ", WSAGetLastError ());
return 1;
} else {
Printf (" getnameinfo returned the hostname=% s \ n ", the hostname);
return 0;
}
}

CodePudding user response:

Solved the building Lord, I also encountered this problem, but use nbtstat command can get normal,
  • Related