通过 sockaddr_in gethostname 等将主机转换为 ip

发布于 2024-10-26 12:45:19 字数 1574 浏览 1 评论 0原文

我需要帮助将主机名转换为 ip 并插入到 sockaddr_in->sin_addr 才能分配给 char。 例如我输入:localhost,它给了我 127.0.0.1

我找到了代码,但我不知道为什么它给了我错误的数字

//---
#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* 类型

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 found code, but I dont know why it gives me wrong numbers

//---
#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);
        }

I also wrote own code but i dont know how transfer from sockaddr to sockaddr_in data:

///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 is a char* type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花伊自在美 2024-11-02 12:45:19

尝试这样的事情:

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

我直接从记忆中编写了这段代码,所以我不能保证它有效,但我很确定它应该没问题。

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.

昇り龍 2024-11-02 12:45:19

我相信这个 C 代码符合您的要求:

#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)
{
    printf("==========\n");
    printf("destination         : %s:%d\n",the_name,port_number);

    struct hostent *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(char **pp = returned_host->h_aliases; *pp; pp++) {
        printf("alias               : %s\n", *pp);
    }

    for(char **pp = returned_host->h_addr_list; *pp; pp++) {
        static char answer[INET_ADDRSTRLEN] = {0};
        static struct sockaddr_in   out_addr = {0};

        inet_ntop(AF_INET, (void *)*pp, answer, sizeof(answer));
        printf("IP address          : %s\n",answer);

        int my_socket = socket(AF_INET,SOCK_STREAM,0);

        if(my_socket<0) {
            perror("socket()");
            return;
        }

        memset(&out_addr,0,sizeof(out_addr));
        out_addr.sin_family=AF_INET;
        out_addr.sin_port=htons(port_number);

        memmove(&out_addr.sin_addr, *pp, sizeof(&out_addr.sin_addr));

        if(connect(my_socket, (struct sockaddr*)&out_addr, sizeof(out_addr))) {
            perror("connect()");
            return ;
        }
    }
}

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;
}

我得到的输出是这样的:

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

希望这会有所帮助。

This C code does I believe what you want it to:

#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)
{
    printf("==========\n");
    printf("destination         : %s:%d\n",the_name,port_number);

    struct hostent *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(char **pp = returned_host->h_aliases; *pp; pp++) {
        printf("alias               : %s\n", *pp);
    }

    for(char **pp = returned_host->h_addr_list; *pp; pp++) {
        static char answer[INET_ADDRSTRLEN] = {0};
        static struct sockaddr_in   out_addr = {0};

        inet_ntop(AF_INET, (void *)*pp, answer, sizeof(answer));
        printf("IP address          : %s\n",answer);

        int my_socket = socket(AF_INET,SOCK_STREAM,0);

        if(my_socket<0) {
            perror("socket()");
            return;
        }

        memset(&out_addr,0,sizeof(out_addr));
        out_addr.sin_family=AF_INET;
        out_addr.sin_port=htons(port_number);

        memmove(&out_addr.sin_addr, *pp, sizeof(&out_addr.sin_addr));

        if(connect(my_socket, (struct sockaddr*)&out_addr, sizeof(out_addr))) {
            perror("connect()");
            return ;
        }
    }
}

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;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文