2011-03-26 72 views
3

我需要帮助将主机名转换为IP并插入到sockaddr_in-> sin_addr以便能够分配给char。 比如我输入:本地主机,这让我127.0.0.1通过sockaddr_in gethostname等将主机转换为IP

我发现的代码,但我不知道为什么它给了我错误的号码

//--- 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <string.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <string.h> 
#include <errno.h> 
#include <stdlib.h> 
#include <time.h> 
#include <sys/fcntl.h> 
#include <netdb.h> 
//--- 

///CZY WPISANO HOST 

     struct in_addr inaddr; 
     inaddr.s_addr = inet_addr(argv[1]); 
     if(inaddr.s_addr == INADDR_NONE) //if sHost is name and not IP 
     { 
      struct hostent* phostent = gethostbyname(argv[1]); 
      if(phostent == 0) 
       bail("gethostbyname()"); 

      if(sizeof(inaddr) != phostent->h_length) 
       bail("problem z inaddr"); // error something wrong, 

      puts(argv[1]); 
      inaddr.s_addr = *((unsigned long*) phostent->h_addr); 

      //strdup(inet_ntoa(inaddr)); 
      srvr_addr = inet_ntoa(adr_srvr.sin_addr); 
      puts(srvr_addr); 
     } 

我也写了自己的代码,但我不知道如何转移从SOCKADDR到有sockaddr_in数据:

///CZY WPISANO HOST 
    if(argv[1][0]>=(char)'a' && argv[1][0]<=(char)'Z') 
    { 
     struct hostent *hent; 
     hent = gethostbyname(argv[1]); 
     adr_srvr.sin_addr = (struct in_addr*)hent->h_addr_list; 

    } 

adr_srvr是char *型

我真的需要帮助,谢谢!

回答

17

尝试这样:

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 */ 
    } 

我写了这个代码直接从我的记忆,所以我不能保证它的作品,但我敢肯定它应该没问题。

+0

这实际上很不错,直接来自记忆。我无法从这样的记忆中做到。必须记住socket()调用,并在填充sockaddr_in结构之前将其清零,因为有些字段不会意外设置为非零。 – 2011-03-26 22:13:55

+4

'gethostbyname'已被弃用,并且即将过渡到ipv6将会带来极大的问题。 *总是*使用现代的getaddrinfo接口。 – 2011-03-27 00:47:43

+0

是的,这是真的。我同意那个。 – 2011-03-27 03:57:45

1

这个shell脚本编译正确的C代码,我相信会做你想让它什么:

========== 
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 

希望这有助于:

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 

我得到的是这样的输出。