Code:
#include <windows.h>
#include <cctype>
#include <iostream>
int main(){
//----------------------
// Declare and initialize variables
hostent* remoteHost;
char* host_name;
unsigned int addr;
//----------------------
// 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());
}
std::cout<<remoteHost.h_addr;
system("PAUSE");
// The remoteHost structure can now be used to
// access information about the host
}
Errors:
Code:
Kompilaator: Consoles
Building Makefile: "C:\Programs\Dev-Cpp\Makefile.win"
make... käivitus
make.exe -f "C:\Programs\Dev-Cpp\Makefile.win" all
g++.exe -c Templates/solaris2.cpp -o Templates/solaris2.o -I"C:/PROGRAMS/DEV-CPP/lib/gcc/mingw32/3.4.2/include" -I"C:/PROGRAMS/DEV-CPP/include/c++/3.4.2/backward" -I"C:/PROGRAMS/DEV-CPP/include/c++/3.4.2/mingw32" -I"C:/PROGRAMS/DEV-CPP/include/c++/3.4.2" -I"C:/PROGRAMS/DEV-CPP/include" -fno-access-control
Templates/solaris2.cpp: In function `int main()':
Templates/solaris2.cpp:40: error: `h_addr_list' has not been declared
Templates/solaris2.cpp:40: error: request for member of non-aggregate type before '[' token
Käivitus peatatud
The part of the winsock.h header file:
Code:
struct hostent {
char *h_name;
char **h_aliases;
short h_addrtype;
short h_length;
char **h_addr_list;
#define h_addr h_addr_list[0]
};