sockets problem

This is a discussion on sockets problem within the Linux Programming forums, part of the Platform Specific Boards category; hi, i have this code : Code: #include <stdio.h>#include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> int main() { char ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    34

    sockets problem

    hi,
    i have this code :

    Code:
    #include <stdio.h>#include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    
    
    int main()
    {
     char *hostname = "ddr";
     struct addrinfo hints, *res;
     struct in_addr *addr;
     int err;
     char gg[50];
     memset(&hints, 0, sizeof(hints));
     hints.ai_socktype = SOCK_STREAM;
     hints.ai_family = AF_INET;
    
    
     if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0) 
     {
      printf("error %d\n", err);
      return 1;
     }
    
    
     addr->s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
      gg = inet_ntoa(addr);
     /*printf("official host name: %s\n",h->h_name);*/
     printf("ip address : %s\n", gg);
    
    
     freeaddrinfo(res);
    
    
     return 0;
    }
    and i want to get the following output:
    /*
    official host name: ddr
    internet address: 127.0.1.1 (7F000101)
    */
    where (7F000101) is by the inet_ntoa() obtained info.

    with this code i can get the desired info:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <netdb.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    
    int main(void)
    {
      struct hostent *h;
      struct in_addr *a;
    
    
      if((h=gethostbyname("ddr"))==NULL) exit(1);
        printf("official host name: %s\n",h->h_name);
    
    
      a=(struct in_addr*)h->h_addr_list[0];
      printf("internet address: %s (%08lX)\n",inet_ntoa(*a),(long unsigned int)ntohl(a->s_addr));
    
    
      exit(0);
     
    }

    but witht the 1 code i am not able for some reason, first of all i am getting the following warning when i compile the 1 code:
    gcc tt.c -o tt
    tt.c: In function ‘main’:
    tt.c:29:5: error: incompatible types when assigning to type ‘char[50]’ from type ‘int’
    make: *** [tt] Error 1

    and i dont know why i getting that error, i think that i am doing everything ok, the inet_ntoa returns a char * and gg is also a char*, so what is the problem?

    i thank you in advance for the help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > tt.c:29:5: error: incompatible types when assigning to type ‘char[50]’ from type ‘int’
    ...
    > gg = inet_ntoa(addr);
    You can't assign arrays.

    When you've made it return a char*, then you use strcpy() to copy that to an array.
    Is 50 chars always enough?


    Also, since it also complains that you're trying to assign from int, it means you didn't declare the function properly.
    See UNIX man pages : inet_ntoa (3)
    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
    Registered User
    Join Date
    May 2012
    Posts
    34
    hi, thank you for the answer, but the function inet_ntoa, which is defined as following:
    char *inet_ntoa(struct in_addr in);

    returns a pointer to a char, so it is valid gg = inet_ntoa(addr); given that gg is also a char* type.

    beside that i have changed the program a little bit:

    Code:
     
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
    
    
    int main()
    {
     char *hostname = "ddr";
     struct addrinfo hints, *res;
     struct in_addr *addr;
     int err;
     char *gg;
     memset(&hints, 0, sizeof(hints));
     hints.ai_socktype = SOCK_STREAM;
     hints.ai_family = AF_INET;
    
    
     if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0) 
     {
      printf("error %d\n", err);
      return 1;
     }
    
    
     addr->s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
      /*gg = inet_ntoa(addr);*/
     /*printf("official host name: %s\n",h->h_name);*/
     printf("ip address : %s\n", inet_ntoa(addr));
    
    
     freeaddrinfo(res);
    
    
     return 0;
    }


    so it is now similar to the other program 2.In Program 2 i have the same printf for the same type and i dont get that error
    printf("internet address: %s (%08lX)\n",inet_ntoa(*a),(longunsigned int)ntohl(a->s_addr));

    and in the program 1 i get that error, which is
    gcc tt.c -o tt
    tt.c: In function ‘main’:
    tt.c:32:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    First, compile with more warnings.
    Code:
    $ gcc -Wmissing-prototypes -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:31:2: warning: implicit declaration of function ‘inet_ntoa’ [-Wimplicit-function-declaration]
    foo.c:31:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
    foo.c:15:8: warning: unused variable ‘gg’ [-Wunused-variable]
    foo.c:28:15: warning: ‘addr’ may be used uninitialized in this function [-Wuninitialized]
    Second, read the manual page. Note the feature macro necessary to expose the interface of inet_ntoa
    Also note the header file arpa/inet.h which you are not including.
    Quote Originally Posted by man
    INET(3) Linux Programmer's Manual INET(3)

    NAME
    inet_aton, inet_addr, inet_network, inet_ntoa, inet_makeaddr, inet_lnaof, inet_netof - Internet address manipulation routines

    SYNOPSIS
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>

    int inet_aton(const char *cp, struct in_addr *inp);

    in_addr_t inet_addr(const char *cp);

    in_addr_t inet_network(const char *cp);

    char *inet_ntoa(struct in_addr in);

    struct in_addr inet_makeaddr(int net, int host);

    in_addr_t inet_lnaof(struct in_addr in);

    in_addr_t inet_netof(struct in_addr in);

    Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

    inet_aton(), inet_ntoa(): _BSD_SOURCE || _SVID_SOURCE
    When the include is fixed, the call is fixed and compiled with the correct command line, I get
    Code:
    $ gcc -Wmissing-prototypes -Wall -D_SVID_SOURCE foo.c
    foo.c: In function ‘main’:
    foo.c:15:8: warning: unused variable ‘gg’ [-Wunused-variable]
    foo.c:28:15: warning: ‘addr’ may be used uninitialized in this function [-Wuninitialized]
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sockets Problem C++
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 08-25-2010, 08:22 PM
  2. Sockets and UDP problem
    By ally153 in forum C Programming
    Replies: 5
    Last Post: 03-20-2006, 05:19 AM
  3. Raw Sockets problem.
    By Mad_guy in forum C Programming
    Replies: 3
    Last Post: 07-03-2005, 04:14 PM
  4. sockets problem
    By lithium in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 07:10 PM
  5. Problem with Raw Sockets
    By Encrypted in forum Linux Programming
    Replies: 2
    Last Post: 04-30-2003, 05:38 PM

Tags for this Thread


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