Use of inet_aton() function

Use of inet_aton() function

Post by Jeff Hank » Sun, 08 Feb 1998 04:00:00



Could someone give me a 4 or 5 line example of code showing the correct
syntax for
using the inet_aton() function.  I have tried nearly every possible
combination in my code
and can't seem to get it to compile.

for example:

struct sockaddr_in temp;
if (inet_aton("147.226.53.6", &(WHAT GOES HERE))
  printf ("works\n");

Thanks for any help you can give me.

Jeff Hanks


 
 
 

Use of inet_aton() function

Post by Andrew Giert » Sun, 08 Feb 1998 04:00:00


[Followups narrowed - don't want to upset the C/C++ people]

 Jeff> Could someone give me a 4 or 5 line example of code showing the
 Jeff> correct syntax for using the inet_aton() function.  I have
 Jeff> tried nearly every possible combination in my code and can't
 Jeff> seem to get it to compile.

 Jeff> for example:

 Jeff> struct sockaddr_in temp;
 Jeff> if (inet_aton("147.226.53.6", &(WHAT GOES HERE))
 Jeff>   printf ("works\n");

    if (inet_aton("147.226.53.6", &temp->sin_addr))
      printf("works\n");

inet_aton wants a pointer to a "struct in_addr" as its second parameter.

See also the example prog I posted a few days ago (an example of
gethostbyaddr(), but it used inet_aton too).

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

Use of inet_aton() function

Post by Karl Steneru » Tue, 10 Feb 1998 04:00:00


int connect_socket(char* address, int port)
{
   struct hostent* host_info;           // returned by gethostbyname()
   int                fd;               // file descriptor
   struct sockaddr_in saddr;            // holds socket info

   // prepare socket info
   memset(&saddr, 0, sizeof(saddr));
   saddr.sin_family = AF_INET;
   saddr.sin_port = htons(port);

   // try to decode dotted quad notation
   if(!inet_aton(address, &saddr.sin_addr))
   {
      // failing that, look up the name
      if( (host_info = gethostbyname(address)) == NULL)
      {
         perror("gethostbyname");
         return -1;
      }
      memcpy(&saddr.sin_addr, host_info->h_addr, host_info->h_length);
   }

   // open the socket
   if( (fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
   {
      perror("socket");
      return -1;
   }

   // connect to the remote site
   if(connect(fd, (struct sockaddr*) &saddr, sizeof(saddr)) < 0)
   {
      perror("connect");
      close(fd);
      return -1;
   }
   return fd;

Quote:}



> Could someone give me a 4 or 5 line example of code showing the correct
> syntax for
> using the inet_aton() function.  I have tried nearly every possible
> combination in my code
> and can't seem to get it to compile.

> for example:

> struct sockaddr_in temp;
> if (inet_aton("147.226.53.6", &(WHAT GOES HERE))
>   printf ("works\n");

> Thanks for any help you can give me.

> Jeff Hanks



 
 
 

1. inet_aton() function problems Any help appreciated!

Hello everyone,

I made a decision to try some linux programming this weekend.  I wanted to
try out the inet_aton function which is supposed to convert a normal ip to
its decimal value right?

here is the problem.  When i run the program and pass it 192.168.0.1 it
returns 16820416.
unfortunately that integer is the decimal value of 1.0.168.192
1.0.0.0 returns 1
0.0.0.1 returns 16777216

everything is backwards!

=(
needlesss to say i am a little confused.  Any help would be greatly
appreciated!

Here is the code i used:

#include <stdio.h>
#include <arpa/inet.h>

int main (int argc, char **argv)
{
    struct in_addr FullAddress;
    char *ipaddr;
    printf("%d\n",argc);
    if(argc<2){
        printf("You did not provide a parameter!\n");
        exit(1);
    }
    else{
        ipaddr = argv[1];
        inet_aton(argv[1], &FullAddress);
        printf("The Decimal value of your ip is: %lu\n", FullAddress);
    }
    return 0;

-Chris Wright

2. what's ppp0?

3. Kdevelop uses can't find QT-library and uses wrong function-names

4. I_PUSH error message when using telnet/rlogin (2.4)

5. function, function...who's got the function?

6. LinuxPPC v4 won't work with my UMAX Pulsar (again)

7. inet_aton

8. Help, Errors in make zImage.

9. inet_aton and inet_ntoa under Solaris7/8

10. inet_aton

11. inet_aton() !?

12. help with inet_aton IP conversion...

13. inet_addr and inet_aton not working on linux