converting host to ip by sockaddr_in gethostname etc

前端 未结 2 2006
名媛妹妹
名媛妹妹 2020-12-28 11:14

i need help converting hostname to ip and inserting to sockaddr_in->sin_addr to be able assign to char. For example i input: localhost and it gives me 127.0.0.1

I fo

相关标签:
2条回答
  • 2020-12-28 11:23

    This shell script compiles correct C code, which I believe will do what you want it to:

    rm -f 1; cat > 1.c <<EOD; gcc -Wall -Werror 1.c -o 1; ./1
    
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <string.h>
    
    void
    do_one(char *the_name,int port_number)
    {
      int                  my_socket;
    
      char               **pointer_pointer;
    
      char                 answer[INET_ADDRSTRLEN];
    
      struct hostent      *returned_host;
    
      struct sockaddr_in   outgoing_address;
    
      printf("==========\n");
    
      printf("destination         : %s:%d\n",the_name,port_number);
    
      returned_host=gethostbyname(the_name);
    
      if(returned_host==NULL)
      {
        fprintf(stderr,"error %d\n",h_errno);
    
        return;
      }
    
      printf("host's official name: %s\n",returned_host->h_name);
    
      for(pointer_pointer=returned_host->h_aliases;
          *pointer_pointer;
          pointer_pointer++
         )
      {
        printf("alias               : %s\n",*pointer_pointer);
      }
    
      for(pointer_pointer=returned_host->h_addr_list;
          *pointer_pointer;
          pointer_pointer++
         )
      {
        inet_ntop(AF_INET,(void *)*pointer_pointer,answer,sizeof(answer));
    
        printf("IP address          : %s\n",answer);
    
        my_socket=socket(AF_INET,SOCK_STREAM,0);
    
        if(my_socket<0)
        {
          perror("socket()");
    
          return;
        }
    
        memset(&outgoing_address,0,sizeof(outgoing_address));
        outgoing_address.sin_family=AF_INET;
        outgoing_address.sin_port=htons(port_number);
    
        memmove(&outgoing_address.sin_addr,
                *pointer_pointer,
                sizeof(&outgoing_address.sin_addr)
               );
    
        if(connect(my_socket,(struct sockaddr*)&outgoing_address,sizeof(outgoing_address)))
        {
          perror("connect()");
    
          return ;
        }
    
        printf("connection established on file descriptor %d\n",my_socket);
      }
    
    } /* do_one() */
    
    int
    main(void)
    {
      do_one("localhost",80);
      do_one("localhost",81);
      do_one("127.0.0.1",80);
      do_one("tiger",80);
      do_one("www.google.com",80);
    
      return 0;
    
    } /* main() */
    
    EOD
    

    The output I got was this:

    ==========
    destination         : localhost:80
    host's official name: localhost
    IP address          : 127.0.0.1
    connection established on file descriptor 3
    ==========
    destination         : localhost:81
    host's official name: localhost
    IP address          : 127.0.0.1
    connect(): Connection refused
    ==========
    destination         : 127.0.0.1:80
    host's official name: 127.0.0.1
    IP address          : 127.0.0.1
    connection established on file descriptor 5
    ==========
    destination         : tiger:80
    host's official name: tiger.x441afea5.org
    alias               : tiger
    IP address          : 10.0.0.1
    connection established on file descriptor 6
    ==========
    destination         : www.google.com:80
    host's official name: www.l.google.com
    alias               : www.google.com
    IP address          : 74.125.229.50
    connection established on file descriptor 7
    IP address          : 74.125.229.52
    connection established on file descriptor 8
    IP address          : 74.125.229.48
    connection established on file descriptor 9
    IP address          : 74.125.229.49
    connection established on file descriptor 10
    IP address          : 74.125.229.51
    connection established on file descriptor 11
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-28 11:28

    Try something like this:

      struct hostent        *he;
      struct sockaddr_in  server;
      int                 socket;
    
      const char hostname[] = "localhost";
    
      /* resolve hostname */
      if ( (he = gethostbyname(hostname) ) == NULL ) {
          exit(1); /* error */
      }
    
      /* copy the network address to sockaddr_in structure */
      memcpy(&server.sin_addr, he->h_addr_list[0], he->h_length);
      server.sin_family = AF_INET;
      server.sin_port = htons(1337);
    
      /* and now  you can connect */
      if ( connect(socket, (struct sockaddr *)&server, sizeof(server) ) {
          exit(1); /* error */
      }
    

    I wrote this code straight from my memory so I cannot guarantee that it works but I am pretty sure it should be OK.

    0 讨论(0)
提交回复
热议问题