Obtains all IP addresses of a computer based on its name.

Source: Internet
Author: User

1. Add command line parameters to the Project

2. Code

# Include "stdafx. H"
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

// Socket programming

# Include "winsock2.h"
# Pragma comment (Lib, "ws2_32.lib ")

 
# Include "ws2tcpip. H" // note location

Int main (INT argc, char ** argv)
{
// Wsastartup (it initiates use of ws2_32.dll by a process) must be called before any socket function is called)
Wsadata;
If (wsastartup (makeword (2, 2), & wsadata )! = 0 ){
Fprintf (stderr, "wsastartup failed: % d \ n", getlasterror ());
Exitprocess (-1 );
}

If (argc! = 2 ){
Fprintf (stderr, "Usage: % s hostname \ n ",
Argv [1]);
Exit (1 );
}

Struct addrinfo * answer, hint, * curr;
Memset (& hint, 0, sizeof (hint ));
Hint. ai_family = af_inet;
Hint. ai_socktype = sock_stream;
Int ret = getaddrinfo (argv [1], null, & hint, & answer );
If (Ret! = 0 ){
Fprintf (stderr, "getaddrinfo: % s \ n ",
Gai_strerror (RET ));
Exit (1 );
}
Char * dest_ip;
For (curr = answer; curr! = NULL; curr = curr-> ai_next ){

Dest_ip = inet_ntoa (struct sockaddr_in *) (curr-> ai_addr)-> sin_addr ));
Fprintf (stdout, "IP: % s \ n", dest_ip );
}
Freeaddrinfo (answer );

Wsacleanup ();
System ("pause ");
Exit (0 );
}

Result:

Add multiple IP addresses:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.