什么ip地址接受退货

发布于 2024-08-09 21:14:46 字数 920 浏览 4 评论 0原文

请参阅以下代码:

accept(sockfd, (struct sockaddr*)&cliaddr, &slen);
cout << inet_ntop(AF_INET, cliaddr.sin_addr, ipv4addr, 100);

我的客户端从本地主机连接。 我在输出中得到一个荒谬的地址。这不是我的 IP 地址。每次我运行代码时我都会得到不同的IP地址。当我 ping 该 IP 地址时,没有得到任何响应。

原因是什么。

我在 Windows Vista 的虚拟机上运行 suse linux。

更新

bzero(&cliaddr, sizeof(cliaddr));
int connfd = accept(sockfd, (struct sockaddr*)&cliaddr, &slen);

if (sem_wait(&mutex) < 0)
    err_sys("sem_init error");

char ipv4addr[100];
cout << inet_ntop(AF_INET, &cliaddr.sin_addr, ipv4addr, 100) << endl;

//const char* p = inet_ntop(AF_INET, &cliaddr.sin_addr, ipv4addr, 100);
//cout << p << endl;

//cout << (void*)p << " " << (void*)ipv4addr << endl;

如果我取消注释这些行,这将返回地址 0.0.0.0

,我会在所有行中得到正确的地址,127.0.0.1

see the following code:

accept(sockfd, (struct sockaddr*)&cliaddr, &slen);
cout << inet_ntop(AF_INET, cliaddr.sin_addr, ipv4addr, 100);

my client connects from localhost.
i get an absurd address in the output. this is not my ip address. everytime i run the code i get a different ip address. when i ping that ip address i don't get any response.

what is the reason.

i am running suse linux on a virtual machine in windows vista.

Update:

bzero(&cliaddr, sizeof(cliaddr));
int connfd = accept(sockfd, (struct sockaddr*)&cliaddr, &slen);

if (sem_wait(&mutex) < 0)
    err_sys("sem_init error");

char ipv4addr[100];
cout << inet_ntop(AF_INET, &cliaddr.sin_addr, ipv4addr, 100) << endl;

//const char* p = inet_ntop(AF_INET, &cliaddr.sin_addr, ipv4addr, 100);
//cout << p << endl;

//cout << (void*)p << " " << (void*)ipv4addr << endl;

this returns address as 0.0.0.0

if i uncomment the lines, i get the correct address in all the lines, 127.0.0.1

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

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

发布评论

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

评论(3

旧伤还要旧人安 2024-08-16 21:14:46

您在调用 inet_ntop() 时缺少第四个参数。这是一个有效的示例:


  int sockfd, fd;
  struct sockaddr_in saddr;
  socklen_t len = sizeof( saddr );
  char addr_buf[INET_ADDRSTRLEN]; /* defined in <netinet/in.h> */

  /* ... socket(), bind(), listen() */

  bzero( &saddr, len );
  if (( fd = accept( sockfd, ( struct sockaddr* )&saddr, &len )) == -1 )
  { perror( "accept" ); exit( 1 ); } /* watch out for EINTR */

  if ( inet_ntop( AF_INET, &saddr.sin_addr, addr_buf,
    INET_ADDRSTRLEN ) == NULL )
  { perror( "inet_ntop" ); exit( 1 ); }

  printf( "accepted connection from [%s:%d]\n",
    addr_buf, ntohs( saddr.sin_port ));
  ...

与网络通信时始终检查错误。

You are missing the 4th parameter in your call to inet_ntop(). Here's a working example:


  int sockfd, fd;
  struct sockaddr_in saddr;
  socklen_t len = sizeof( saddr );
  char addr_buf[INET_ADDRSTRLEN]; /* defined in <netinet/in.h> */

  /* ... socket(), bind(), listen() */

  bzero( &saddr, len );
  if (( fd = accept( sockfd, ( struct sockaddr* )&saddr, &len )) == -1 )
  { perror( "accept" ); exit( 1 ); } /* watch out for EINTR */

  if ( inet_ntop( AF_INET, &saddr.sin_addr, addr_buf,
    INET_ADDRSTRLEN ) == NULL )
  { perror( "inet_ntop" ); exit( 1 ); }

  printf( "accepted connection from [%s:%d]\n",
    addr_buf, ntohs( saddr.sin_port ));
  ...

Always check for errors when talking to network.

橙味迷妹 2024-08-16 21:14:46

我未经证实的猜测是,您返回的是 IP v6 地址而不是 v4 地址,因此您的转换已关闭。

您可能想尝试使用 netstat 找出客户端的端口(通常会得到 1025 到 65535 之间的随机端口号),并查看该端口的十六进制值是否出现在 cliaddr 的十六进制表示形式中的某个位置。如果客户端端口与您认为的客户端地址之间存在关联,则您的转换不正确。

My unsubstantiated guess is that you're getting IP v6 addresses back instead of v4, so your conversion is off.

You might want to try using netstat to find out the client's port (you usually get a sort-of-random port number between 1025 and 65535) and see if the hex value of that appears somewhere in the hex representation of cliaddr. If there's a correlation between client port and what you believe to be the client address, then your conversion is incorrect.

罗罗贝儿 2024-08-16 21:14:46

我的下一个猜测:

  On success, inet_ntop() returns a non-null pointer  to  dst.   NULL  is
       returned if there was an error, with errno set to indicate the error.

是cout。<<足够聪明来取消引用正在返回的指针,或者您正在打印指针?

My next guess:

  On success, inet_ntop() returns a non-null pointer  to  dst.   NULL  is
       returned if there was an error, with errno set to indicate the error.

Is cout.<< clever enough to dereference the pointer that's being returned, or are you printing out the pointer?

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