0

This is the server of a socket communication. When the client sends a URL to the server, the server sends the IP to client. When it runs hptr = gethostbyname(buffer), it always returns NULL. Why? Thank you!

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main( int argc, char *argv[] )
{
    int sockfd, streamfd, addr_size, status;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    struct in_addr **addr_list;

    struct hostent *hptr;
    char  *ptr, **pptr;
    char  str[32];
    sockfd = socket(PF_INET, SOCK_STREAM, 0);

    if (sockfd < 0)
    {   
       perror("ERROR opening socket");
       exit(1);
    }

   /* Initialize socket structure */
   bzero((char *) &serv_addr, sizeof(serv_addr));

   serv_addr.sin_family = PF_INET;
   serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
   serv_addr.sin_port = htons(1234);

  /* Now bind the host address using bind() call.*/
  if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  {
  perror("ERROR on binding");
  exit(1);
  }

  /* Now start listening for the clients, here process will
   * go in sleep mode and will wait for the incoming connection
  */

  listen(sockfd,10);
  addr_size = sizeof(cli_addr);

  /* Accept actual connection from the client */
  while(1){
       streamfd = accept (sockfd, (struct sockaddr *) &cli_addr, &addr_size);   

       status = read (streamfd, buffer, 255);   
       printf ("string from net: %s\n", buffer);



       if((hptr = gethostbyname(buffer)) == NULL)
       {
           printf("gethostbyname error for host:%s\n", buffer);
           return 0; 
       }



       printf("IP Address:%s\n",inet_ntoa(*((struct in_addr *)hptr->h_addr)));
       close(streamfd); 
    }
    return 0;
}
Ben Ruijl
  • 4,973
  • 3
  • 31
  • 44
DaveyTao
  • 33
  • 6
  • 2
    What does `string from net:` say? – donjuedo Jun 23 '15 at 13:08
  • What does the client send? Does it include the null terminator? Does it contain a newline (for example if the string in the client was input with `fgets`)? Does it contain any leading/trailing white-space? Does `read` succeed? What does `read` return? – Some programmer dude Jun 23 '15 at 13:10
  • @donjuedo - probably nothing and/or garbage because of assuming messages longer than one byte can be transferred over TCP, ingoring the result returned by read() and the lack of secure null-termination, (ie. all the usual suspects). – Martin James Jun 23 '15 at 13:12
  • I want to know whether the parameter of gethostbyname() is wrong?The "buffer" is a array. – DaveyTao Jun 23 '15 at 13:13
  • Yes, it is almost certainly wrong. – Martin James Jun 23 '15 at 13:21
  • [sigh] what does you debugger say is in the buffer parameter at the time of the gethostbyname() call? Is it what you expect? Is it complete? Is it null-terminated? Why do you think that read() returns a value? – Martin James Jun 23 '15 at 13:25
  • @donjuedo asked 'What does string from net: say?' - well? – Martin James Jun 23 '15 at 13:26

2 Answers2

1

For some reason you assume that:

  1. read always succeeds and returns you a complete chunk that the sender has sent in one send call.
  2. Data read is zero-terminated.

Both of these assumptions are incorrect.

You need to make sure that you read a complete message or a line (whatever your wire protocol) and that your strings are zero-terminated (people often do not send zero terminators over the wire).

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
1

the posted code doesn't even come close to compiling.

1) bzero() needs (note the plural 'strings')
    #include <strings.h> for bzero()
2) accept() parameter 3 has incorrect signedness
3) accept() return parameter should be socklen_t* but is int*
4) on linux for read() needs 
    #include <unistd.h>
5) inet_ntoa() needs 
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
6) several unused variables and parameters.
7) variable 'status' set but not used

Strongly suggest when compiling to enable all warnings

(for gcc, as a minimum, '-Wall -Wextra -pedantic')

Then fix the warnings.

please consistently indent the code, for readability by us humans

many system functions return a value that can be used to determine if the operation was successful. read() is such a function.

The code needs to perform error checking on the returned value from read()

user3629249
  • 16,402
  • 1
  • 16
  • 17