Winsock converts host names and IP addresses

Source: Internet
Author: User
# Include <winsock2.h>
# Include <stdio. h>
# Include <conio. h> wsadata; bool initsocket ()
{
Word wversionrequested;
Int err;

Wversionrequested = makeword (2, 0); // query Winsock 2.0
Err = wsastartup (wversionrequested, & wsadata); // load and initialize Windows Sockets DLL
If (Err! = 0 ){
Printf ("no windows socket dynamic library! \ N ");
Getch ();
Return false;
}
If (lobyte (wsadata. wversion )! = 2 | hibyte (wsadata. wversion )! = 0 ){
Printf ("Windows Socket 2 is required! \ N ");
Getch ();
Wsacleanup ();
Return false;
}
Return true;
} Int main (INT argc, char * argv [])
{
// Local variable Declaration
// Wsadata;
Lphostent;
DWORD dwipaddr;
Lpstr szipaddr;
Char buf_domname [24] = "";
Char buf_ip [15] = ""; if (! Initsocket ())
Printf ("Winsock start error! ");
Else
Printf ("program is running: \ n"); printf ("DNS resolve test program's ready ");
Printf ("(no input equals Local Machine) ...... \ n ");
Printf ("\ n"); // start domain name resolve here.
Printf ("Please enter the domain name :");
Gets (buf_domname); // perform resolve here.
Lphostent = gethostbyname (buf_domname );
If (! Lphostent)
{
// Error occurs!
Printf ("cocould not get IP address! ");
Printf ("\ n ");
}
Else
{
// Convert & Output
Szipaddr = inet_ntoa (* (lpin_addr) * (lphostent-> h_addr_list ));
Printf (szipaddr );
Printf ("\ n ");
} // Start IP resolve here.
Printf ("Please enter the IP Address :");
Gets (buf_ip); dwipaddr = inet_addr (buf_ip );
If (dwipaddr = inaddr_none)
// Ip address validation failed!
Printf ("invalid Internet address! \ N ");
Else
{
Lphostent = gethostbyaddr (lpstr) & dwipaddr, sizeof (dwipaddr), af_inet );
If (! Lphostent)
{
// Error occures!
Printf ("cocould not get host name! ");
Printf ("\ n ");
}
Else
{
// Output
Printf (lphostent-> h_name );
Printf ("\ n ");
}
} // Shut down the socket
Wsacleanup ();
Printf ("\ n ");
Return 0 ;}

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.