This is a winsock program designed to prompt the user to enter a domain name.
The program is then supposed to return to the user a resolved ip address of that domain name. The user should also be able to enter an IP address and receive back a domain name.
While the program prompts the user to enter the domain name, all that is ever returned is error#:0
Can anyone shed light on why??
See code
HTML Code:
// Declare and initialize variables
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#include <iostream>
int main(int argc, char **argv)
{
hostent* remoteHost;
char* host_name;
unsigned int addr;
//------------------------------------------------------------------------------------------------
//Initialize WINSOCK 2
WSADATA wsaData; //structure that holds information about current wsock version.
if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0) //start wsock version 2.2. Function returns
//0 if successful, otherwise, there was an error that needs to be reported.
{
std::cout << "WSAStartup failed: " << WSAGetLastError(); //report the error if there is one
return 1; //exit the program.
}
//-------------------------------------------------------------------------------------------------
// User inputs name of host
printf("Input name of host: ");
//-------------------------------------------------------------------------------------------------
// Allocate 64 byte char string for host name
host_name = (char*) malloc(sizeof(char)*64);
fgets(host_name, 64, stdin);
//-------------------------------------------------------------------------------------------------
// If the user input is an alpha name for the host, use gethostbyname()
// If not, get host by addr (assume IPv4)
if (isalpha(host_name[0])) { /* host address is a name */
// if hostname terminated with newline '\n', remove and zero-terminate
if (host_name[strlen(host_name)-1] == '\n')
host_name[strlen(host_name)-1] = '\0';
remoteHost = gethostbyname(host_name);
}
else {
addr = inet_addr(host_name);
remoteHost = gethostbyaddr((char *)&addr, 4, AF_INET);
}
if (WSAGetLastError() != 0) {
if (WSAGetLastError() == 11001)
printf("Host not found...\nExiting.\n");
}
else
printf("error#:%ld\n", WSAGetLastError());
}
You are correct.
That fixed it. Now it outputs information.
However, the information is NOT the ip address like I thought.
I want the user to input a domain name, like www.google.com
and have the program resolve the DNS entry (via gethostby.... function) and output: 173.194.75.104
Instead, it just kind of echoes what the user entered. See below:
Input name of Host: www.google.com
Remote host name: www.google.com
Sorry. Change all the %i in the printf to %u. Printf is treating the numbers as signed so printing some as negative. They are unsigned numbers. I only gave an example of how to extract the ip address from the returned data.
Er.. NO! Thats not how things work unfortunately. If you enter ip address 98.139.183.24 and get ir2.fp.vip.bf1.yahoo.com then your program is working! Try this. At the command line type nslookup and at the > prompt type www.yahoo.com. Then at the next > prompt type in the address just shown. Type exit to exit the program. Going from an ip address to a domain name is called reverse DNS lookup. To understand what is going on look at
But the answer you get back is whatever has been designated for the in-addr.arpa address! What you are expecting is what is known as forward-confirmed reverse DNS. Some sites do and some don't!
So I have new code here that I need to do the same with.
1. using the gethostbyname function, prompt user for domain name, which will be entered as a string to be resolved
2. return the ip address of the domain entered.
3. use inet_ntoa to format and display the ip address
4. if an error is returned, print a message for the error using the WSAGetLastError
HTML Code:
// C++ DNS Processing (C++ Windows Version)
#include <iostream>
using namespace std;
#include <winsock.h>
int main(int argc, char **argv)
{
// Add the WSAStartup code...
// Winsock 1.1
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
// Define the structure for the returned data
struct hostent *hostptr;
// Test case... change to other names or define in input variable
char *exampleHostName = "www.sunybroome.edu";
// Return code for the Winsock network startup
int nRet;
// Initialize the Winsock environment
nRet = WSAStartup(wVersionRequested, &wsaData);
// Check to make sure the environment initialized successfully
if(wsaData.wVersion != wVersionRequested)
{
cout << "Wrong version\n";
return 0;
}
if (hostptr = gethostbyname (exampleHostName) )
{
//
// The IP address is now in hostptr->h_addr
// Print out the contents of hostent structure
// in an easily readable form with labels. For
// example, the host name can be output using:
cout << "Host name: " << hostptr->h_name << endl;
}
else
{
// An error has occurred. Display an informative
// error message using the result of WSAGetLastError()
}
}
Bookmarks