Thread: gethostbyname() won't work...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    22

    Unhappy gethostbyname() won't work...

    I am trying to connect to a server that I create. My server works fine if the client is in the same host because I use this in the client code:

    Code:
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    Now, when I try to connect to the server, being in a different host, I do this:
    Code:
    struct hostent *he;
    ..
    
    char *servername = argv[1];
    he = gethostbyname(servername);
    memcpy(&serverAddr.sin_addr, he->h_addr_list[0], he->h_length);
    	
    // Connecting to the server
    puts("1");
    if (connect(mySock, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1) {
    	puts("2");
            perror("connect");
            return EXIT_FAILURE;
    }
    puts("Connected to the server.");
    The problem is that the "1" gets printed, and the "Connected..." message never appears.. So it gets stuck in connect. I am 100% sure it's something wrong with that gethostbyname, I must be using it wrong.

    Can anyone enlighten me?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://beej.us/guide/bgnet/
    Probably find a working example in there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Quote Originally Posted by headbr View Post
    I am trying to connect to a server that I create. My server works fine if the client is in the same host because I use this in the client code:

    Code:
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    Now, when I try to connect to the server, being in a different host, I do this:
    Code:
    struct hostent *he;
    ..
    
    char *servername = argv[1];
    he = gethostbyname(servername);
    memcpy(&serverAddr.sin_addr, he->h_addr_list[0], he->h_length);
    	
    // Connecting to the server
    puts("1");
    if (connect(mySock, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1) {
    	puts("2");
            perror("connect");
            return EXIT_FAILURE;
    }
    puts("Connected to the server.");
    The problem is that the "1" gets printed, and the "Connected..." message never appears.. So it gets stuck in connect. I am 100% sure it's something wrong with that gethostbyname, I must be using it wrong.

    Can anyone enlighten me?
    INADDR_ANY is not supposed to be a remote destination; its for listening sockets. using it on a client socket has the same effect like using 127.0.0.1, but has the side effect of making your code look weird. I'm not sure but it might also cause undefined behaviour since INADDR_ANY means 0.0.0.0, which would mean that all network interfaces are querried. So only use it on listening (server) sockets, which are supposed to listen on all interfaces.

    The way you call gethostbyname() is perfectly fine. I even checked the code snippet here and it seems to work, so it might be something thats not on this snipped. Did you forget setting sin_family or sin_port on serverAddr?
    By the way, when your connect() fails, perror() prints the error to stderr.. did you consider taking a look at that? Would be helpful if you posted the output
    Last edited by mkruk; 08-14-2008 at 06:02 AM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    check the return value of gethostbyname

    check that the he->h_addr_list[0] is not null before using it

    print value of he->h_addr_list[0] to see what IP is selected for connecting
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM