我正在尝试读取有关特定主机的所有信息并打印出每条信息。 我可以阅读并打印出所有地址,但我没有阅读任何 ai_canonname!
起初我以为我的示例 (www.google.com|www.irs.gov|...) 没有教规名称,但过了一会儿我发现我根本没有得到任何名称。 您认为我做错了什么,或者您有可行的示例吗?
这是我的代码,
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <sys/time.h>
#include <arpa/inet.h>
int main(int argc, char **argv)
{
struct addrinfo *result, *rp, hints;
int error;
char canonname[32][256];
int canonnum = 0;
char ip[32][64];
int ipnum = 0;
struct timeval tv;
uint64_t starttime, endtime;
if(argc<2)
{
printf("Usage: %s <address>\n", argv[0]);
return 0;
}
/* Record Start time */
gettimeofday(&tv, NULL);
starttime = tv.tv_usec;
memset(&hints, 0, sizeof(hints));
memset(canonname, 0, 32*256*sizeof(char));
memset(ip, 0, 32*64*sizeof(char));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
error = getaddrinfo(argv[1], NULL, &hints, &result);
if (error != 0)
{
if (error == EAI_SYSTEM)
{
perror("getaddrinfo");
}
else
{
fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
}
return -1;
}
strcpy(canonname[0], "");
if(result->ai_canonname != NULL)
strcpy(canonname[0], result->ai_canonname);
canonnum++;
for(rp = result; rp != NULL; rp = rp->ai_next) {
if(rp !=result && rp->ai_canonname != NULL)
{
if(strcmp(rp->ai_canonname, "")!=0)
{
strcpy(canonname[canonnum],rp->ai_canonname);
canonnum++;
}
}
struct sockaddr_in *inaddr_ptr;
if (rp->ai_addr->sa_family == AF_INET)
inaddr_ptr = (struct sockaddr_in *)rp->ai_addr;
sprintf(ip[ipnum],"%s\n", inet_ntoa(inaddr_ptr->sin_addr));
ipnum++;
}
/* Gets the end time and prints out the execution time */
gettimeofday(&tv, NULL);
endtime = tv.tv_usec;
printf("Execution time: %llu milliseconds\n",(endtime - starttime)/100);
printf("Official name: %s\n", canonname[0]);
printf("Aliases:\n");
for(int i=1;i<canonnum;i++)
printf("%s\n",canonname[i]);
printf("Addresses:\n");
for(int i=0;i<ipnum;i++)
printf("%s",ip[i]);
freeaddrinfo(result);
return 0;
}
最佳答案
getaddrinfo() 的规范说你应该添加:
hints.ai_flags = AI_CANONNAME;
你会得到你想要的。
$ ./gai www.ibm.com
Execution time: 4499 milliseconds
Official name: www.ibm.com
Aliases:
Addresses:
129.42.60.216
$ ./gai www.google.com
Execution time: 248 milliseconds
Official name: www.google.com
Aliases:
Addresses:
74.125.239.50
74.125.239.49
74.125.239.48
74.125.239.52
74.125.239.51
$ ./gai www.irs.gov
Execution time: 2872 milliseconds
Official name: 63-146-70-67.dia.static.qwest.net
Aliases:
Addresses:
63.146.70.67
63.146.70.96
$ ./gai www.irs.gov
Execution time: 2299 milliseconds
Official name: 63-146-70-96.dia.static.qwest.net
Aliases:
Addresses:
63.146.70.96
63.146.70.67
$
我不确定如何理解 IRS 不断变化的身份。看起来,规范并不意味着所有这些都规范。
关于c++ - getaddrinfo,我没有得到任何 canonname,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18884251/
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle