gethostbyname

This is a discussion on gethostbyname within the Networking/Device Communication forums, part of the General Programming Boards category; Consider this code: Code: #include <stdio.h> #include <string.h> #include <Winsock2.h> int main(int argc, char *argv[]) { struct hostent *h; if(argc ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    gethostbyname

    Consider this code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <Winsock2.h>
    
    int main(int argc, char *argv[])
    {
    	struct hostent *h;
    	if(argc !=2)
    	{
    		fprintf(stderr,"Argument error!");
    		exit(1);
    	}
    	h=gethostbyname(argv[1]);
    	if(h==NULL)
    	{
    		fprintf(stderr,"gethostbyname error!");
    		exit(1);
    	}
    	
    	printf("\nHost name: %s",h->h_name);
    	printf("IP adress: %s",inet_ntoa(*((struct in_addr *)h->h_addr)));
    
    }
    This also an example from Beej's tutorial.

    Of coure that I'll get error "gethostbyname error!"

    I don't know what is the purpose of this example.
    This, just like other examples simply don't work.
    Maybe I'm doing something wrong?
    After server and telnet example this happened.
    It's seems that networking is simply too hard (not to understand, but to make it work)!
    If someone can explain me what I'm doing wrong (except the decision to learn this)

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You need to initialize winsock before using any winsock function:

    Code:
    int main(int argc, char *argv[])
    {
    	struct hostent *h;
    	WORD wVersionRequested;
    	WSADATA wsaData;
    	int err;
     
    	wVersionRequested = MAKEWORD( 2, 2 );
     
    	err = WSAStartup( wVersionRequested, &wsaData );
    	if ( err != 0 ) {
    	    printf("Failed to initialize!\n");
    	    return -1;
    	}
    
    	if(argc !=2)
    	{
    		fprintf(stderr,"Argument error!");
    		exit(1);
    	}
    	h=gethostbyname(argv[1]);
    	if(h==NULL)
    	{
    		fprintf(stderr,"gethostbyname error!");
    		exit(1);
    	}
    	
    	printf("\nHost name: %s",h->h_name);
    	printf("IP adress: %s",inet_ntoa(*((struct in_addr *)h->h_addr)));
    
    }

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    I did it, but error was that I entered in command line:

    "http://..."

    Sorry for trouble

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can't enter "http://". The "http://" tells a browser that the url is using the http protocol(rather than ftp for example, which uses "ftp://"). It is not part of the server name.

    Here are some valid server names:
    cboard.cprogramming.com
    alltheweb.com
    www.cprogramming.com

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 maxorator in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2005, 01:08 PM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21