帮助此代码(给出 STATUS_ACCESS_VIOLATION )

发布于 2024-11-02 05:03:07 字数 1155 浏览 0 评论 0原文

下面是一段代码,我想显示用户输入的地址的IP地址,但它给了我STATUS_ACCESS_VIOLATION,我不明白其原因。

它编译得很好。这是

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

// Link to ws2_32.lib
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
struct protoent *proto=NULL;
int main(int count, char *strings[])
{   
  int pid;
  struct hostent *host;
  struct sockaddr_in addr;
  if ( count != 3 )
  {
     printf("usage: %s <addr> < Adressflag = 0 for name and 1 for IP  > /n", strings[0] );
     exit(0);
  }
 if ( count == 3 )
 {
     int flag = atoi(strings[2]);
     printf("flag is %d",flag);
     if ( flag == 0)
     {
         pid = getpid();
         proto = getprotobyname("ICMP");
         host = gethostbyname(strings[1]);
         bzero(&addr, sizeof(addr));
         addr.sin_family = host->h_addrtype;
         addr.sin_port = 0;
         addr.sin_addr.s_addr = *(long*)host->h_addr;
         //traceroute(&addr);
         printf("IP of %s is %s",host->h_name , host-> h_addr);
     }
     else
     {

     }
 }
 return 0;
}

我在 cygwin 中运行的。有人知道这个错误吗?有人能告诉我我做错了什么吗?

Below is a code where I want to display the ip address of the address entered by the user but it gives me STATUS_ACCESS_VIOLATION and I am not understanding the reason for it.

It compiles fine. Here is it

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

// Link to ws2_32.lib
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
struct protoent *proto=NULL;
int main(int count, char *strings[])
{   
  int pid;
  struct hostent *host;
  struct sockaddr_in addr;
  if ( count != 3 )
  {
     printf("usage: %s <addr> < Adressflag = 0 for name and 1 for IP  > /n", strings[0] );
     exit(0);
  }
 if ( count == 3 )
 {
     int flag = atoi(strings[2]);
     printf("flag is %d",flag);
     if ( flag == 0)
     {
         pid = getpid();
         proto = getprotobyname("ICMP");
         host = gethostbyname(strings[1]);
         bzero(&addr, sizeof(addr));
         addr.sin_family = host->h_addrtype;
         addr.sin_port = 0;
         addr.sin_addr.s_addr = *(long*)host->h_addr;
         //traceroute(&addr);
         printf("IP of %s is %s",host->h_name , host-> h_addr);
     }
     else
     {

     }
 }
 return 0;
}

I am running this in cygwin. Is anyone aware of the error and can anyone tell what am I doing wrong ??

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

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

发布评论

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

评论(2

梦明 2024-11-09 05:03:07

您真的需要前面的 * 吗?

addr.sin_addr.s_addr = *(long*)host->h_addr

这意味着您正在取消引用某些随机地址。

您也检查主机是否为空。

Do you really need the * on the front?

addr.sin_addr.s_addr = *(long*)host->h_addr

That means you are de-referencing some random address.

You are also not checking host for null.

单身情人 2024-11-09 05:03:07
addr.sin_addr.s_addr = *(long*)host->h_addr

您不能仅将 char* 转换为 long* 来从字符串中获取数字,而且您可能也不应该取消引用。

addr.sin_addr.s_addr = *(long*)host->h_addr

You can't just cast a char* to long* to get a number from a string, and you probably shouldn't be dereferencing, either.

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