Home > Software engineering >  Access to the machine IP address, there was a problem to use the getaddrinfo function, a great god,
Access to the machine IP address, there was a problem to use the getaddrinfo function, a great god,

Time:09-17

Access to the machine IP address, use the getaddrinfo function, the function returns a value of 11003 (EAI_FAIL), in which the native gethostname name and I set up the computer! Want to ask why god complains, what reason? How to change? (I have already been set in the computer the IP for 192.168.250.201, want to pass the getaddrinfo function to get the address right now! Use VS2015, based on the MFC dialog box)

CodePudding user response:

After ai_family=AF_INET, as to such mistakes, 11003!

CodePudding user response:

After defining FW3 initialization
Memset (& amp; FW3, 0, sizeof (struct addrinfo));

CodePudding user response:

If it is a multiple network card, can have more than one address,

CodePudding user response:

Reference on the MSDN example code
 
# undef UNICODE

#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;
Int iResult;
INT iRetval;

DWORD dwRetval;

int i=1;

Struct addrinfo * result=NULL;
Struct addrinfo * PTR=NULL;
Struct addrinfo hints.

Struct sockaddr_in * sockaddr_ipv4;
//struct: sockaddr_in6 * sockaddr_ipv6;
LPSOCKADDR sockaddr_ip;

Char ipstringbuffer [46];
DWORD ipbufferlength=46;

//Validate the parameters
If (arg c! {
=3)Usage: printf (" % s & lt; Hostname> \ n ", argv [0]);
Printf (" getaddrinfo provides protocol - independent translation \ n ");
Printf (" from an ANSI host name to an IP address \ n ");
Printf (" % s example usage \ n ", argv [0]).
www.contoso.com 0 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;
}

//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Setup the hints address info structure
//which is passed to the getaddrinfo () function
ZeroMemory (& amp; Hints, sizeof (hints));
Hints. Ai_family=AF_UNSPEC;
Hints. Ai_socktype=SOCK_STREAM;
Hints. Ai_protocol=IPPROTO_TCP;

Printf (" Calling getaddrinfo with following parameters: \ n ");
Printf (" \ n \ tnodename=% s ", argv [1]).
Printf (" \ tservname (or port)=% s \ n \ n ", argv [2]).

//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Call getaddrinfo (). If the Call succeeds,
//the result variable will hold a linked list
//of addrinfo structures containing the response
//information
DwRetval=getaddrinfo (argv [1], argv [2], & amp; Hints, & amp; The result);
If (dwRetval!=0) {
Printf (" getaddrinfo failed with error: % d \ n ", dwRetval);
WSACleanup ();
return 1;
}

Printf (" getaddrinfo returned success \ n ");

//Retrieve the each address and print out the hex bytes
For (PTR=the result; PTR!=NULL; PTR=PTR - & gt; Ai_next) {

Printf (" getaddrinfo response % d \ n ", i++);
Printf (" \ n \ tFlags: 0 x % x ", PTR - & gt; Ai_flags);
Printf (" \ tFamily: ");
The switch (PTR - & gt; Ai_family) {
Case AF_UNSPEC:
Printf (" Unspecified \ n ");
break;
Case AF_INET:
Printf (" AF_INET (IPv4) \ n ");
Sockaddr_ipv4=(struct sockaddr_in * PTR - & gt; Ai_addr;
Printf (" \ n \ tIPv4 forum "% s",
The inet_ntoa (sockaddr_ipv4 - & gt; Sin_addr));
break;
Case AF_INET6:
Printf (" AF_INET6 (IPv6) \ n ");
//the InetNtop function is available on Windows Vista and later
//sockaddr_ipv6=(struct) : sockaddr_in6 * PTR - & gt; Ai_addr;
//printf (" \ n \ tIPv6 forum "% s",
//InetNtop (AF_INET6, & amp; Sockaddr_ipv6 - & gt; Sin6_addr ipstringbuffer, 46));

//We use WSAAddressToString since it is supported on Windows XP and later
Sockaddr_ip=(LPSOCKADDR) PTR - & gt; Ai_addr;
//The buffer length is changed by each call to WSAAddresstoString
//So we need to set it for each iteration through the loop for safety
Ipbufferlength=46;
IRetval=WSAAddressToString (sockaddr_ip (DWORD PTR) - & gt; Ai_addrlen, NULL,
Ipstringbuffer, & amp; Ipbufferlength);
If (iRetval)
Printf (" WSAAddressToString failed with % u \ n ", WSAGetLastError ());
The else
Printf (" \ n \ tIPv6 forum "% s", ipstringbuffer);
break;
Case AF_NETBIOS:
Printf (" AF_NETBIOS (NetBIOS) \ n ");
break;
Default:
Printf (" Other % ld \ n ", PTR - & gt; Ai_family);
break;
}
Printf (" \ tSocket type: ");
The switch (PTR - & gt; Ai_socktype) {
Case 0:
Printf (" Unspecified \ n ");
break;
Case SOCK_STREAM:
Printf (" SOCK_STREAM (stream) \ n ");
break;
Case SOCK_DGRAM:
Printf (" SOCK_DGRAM (datagram) \ n ");
break;
Case SOCK_RAW:
Printf (" SOCK_RAW (raw) \ n ");
break;
Case SOCK_RDM:
Printf (" SOCK_RDM (reliable message datagram) \ n ");
break;
Case SOCK_SEQPACKET:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related