gethostbyname() won't work...

This is a discussion on gethostbyname() won't work... within the Networking/Device Communication forums, part of the General Programming Boards category; I am trying to connect to a server that I create. My server works fine if the client is in ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,553
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    FOSS Enthusiast
    Join Date
    Jun 2008
    Location
    Germany
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 03:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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