Thread: GetHostByName

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    GetHostByName

    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]
    };

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    the program has two problems.
    1. it does not include winsock.h. you have to include that in addition to windows.h

    2. remostHost is a pointer, so use pointer operator, not the dot operator.
    Code:
    std::cout<<remoteHost->h_addr;

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Thanks

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Well, I get illegal operation message :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gethostbyname() won't work...
    By headbr in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-14-2008, 06:51 AM
  2. gethostbyname
    By TenFold in forum Networking/Device Communication
    Replies: 11
    Last Post: 05-06-2006, 04:41 AM
  3. gethostbyname allways NULL
    By BianConiglio in forum Windows Programming
    Replies: 6
    Last Post: 08-18-2005, 01:27 AM
  4. Replies: 1
    Last Post: 09-11-2004, 08:52 AM
  5. WinSock and gethostbyname() won't return correctly...
    By SyntaxBubble in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2002, 12:08 PM