linux下gethostbyname通过域名获取IP的例子

【Linux编程】gethostbyname 获取网站域名的IP地址解析 实现 代码: #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/inet.h> int main(int argc, char **a... 阅读详情
#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


int main(int argc, char *argv[])
{
    int i;
    struct hostent *he;
    struct in_addr **addr_list;


    if (argc != 2) {
        fprintf(stderr,"usage: ghbn hostname\n");
        return 1;
    }


    if ((he = gethostbyname(argv[1])) == NULL) {  // get the host info
        herror("gethostbyname");
        return 2;
    }


    // print information about this host:
    printf("Official name is: %s\n", he->h_name);
    printf("    IP addresses: ");
    addr_list = (struct in_addr **)he->h_addr_list;
    for(i = 0; addr_list[i] != NULL; i++) {
        printf("%s ", inet_ntoa(*addr_list[i]));
    }
    printf("\n");


    return 0;
}
linux通过域名获取ip地址 通过域名向远程的DNS服务器获取ip地址,程序中也把通过ip向DNS获取域名的逻辑写完了,但由于程序中把获取类型写死了,因此本程序在没有修改源码的情况下只支持通过域名解析ip的需求。使用方法在程序开头有备注。 立即下载

相关推荐

从零配置XDMA:手把手教你PCIe BAR与AXI总线的地址转换(基于Vivado2023)

本文基于Vivado2023,手把手教你配置XDMA IP核,深入解析PCIe BAR与AXI总线地址转换的核心原理。通过实战步骤,详细说明如何正确设置BAR参数(如32/64位、预取使能)及地址映射,确保主机能稳定访问FPGA侧寄存器与内存,是解决PCIe通信调试难题的关键指南。

2w3e4r5t6y的博客 1213

域名解析——gethostbyname()函数及socket客户端域名解析

###gethostbyname()函数说明 gethostbyname()——用域名或主机名获取IP地址 头文件: #include <netdb.h> #include <sys/socket.h> 函数原型: struct hostent *gethostbyname(const char *name); 这个函数的传入值是域名或者主机名。返回值是一个hostent的结构体。如果函数调用失败,返回NULL。结构如下: struct hostent { cha

ZHENYU的博客 5165

【YOLO 系列】基于YOLO V8的智能车载交通标志识别系统【python源码+Pyqt5界面+数据集+训练代码】

本文详细介绍了基于YOLO V8构建智能车载交通标志识别系统的完整流程。从环境搭建、数据集准备、模型训练到PyQt5图形界面开发,提供了Python源码和实战代码,帮助开发者快速实现高精度、实时的交通标志检测应用,提升驾驶辅助系统的开发效率。

wdx012345的博客 966

Linux如何查找域名IP地址

这篇文章主要介绍了在Linux终端中如何查找域名IP地址,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。交互模式允许用户查询名称服务器以获取有关各种主机和域的信息,或打印域中的主机列表。dig 命令代表 “域名信息抓手Domain Information Groper”,它是一个功能强大且灵活的命令行工具,用于查询 DNS 名称服务器。它基于发送的数据包、接收的数据包、丢失的数据包,通常包含最小/平均/最大时间来汇总统计结果。...

学亮编程手记 5955

H3C交换机Console口连接全攻略:从接线到PuTTY配置(附常见问题排查)

本文详细介绍了H3C交换机Console口的连接与配置方法,包括硬件线缆选择、驱动安装、PuTTY参数设置及常见问题排查。特别针对Console转USB线缆的兼容性和驱动安装难点提供实用建议,帮助网络工程师快速掌握串联调试技巧,解决连接中的黑屏、乱码等典型问题。

weixin_42549451的博客 674

豆包大模型API调用两种方法

ps: 因为是自用,所以这里图方便采用硬编码密钥的形式;如果有需求/安全风险,建议参考官方教程设置ARK_API_KEY环境变量的方式。坏处是:看别人的教程走了一大圈弯路hh;基础调用的话方法一就可以满足了,也比较方便快捷,容易上手。将 <Model> 替换为 Endpoint ID即可。创建就按照提示填写信息就行,需要实名认证,有免费额度。绿框框里就是Endpoint ID。

kkkkkkkkkasey的博客 1万+

gethostbyname() -- 用域名或主机名获取IP地址

    #include     #include     struct hostent *gethostbyname(const char *name);    这个函数的传入值是域名或者主机名,例如"www.google.cn"等等。传出值,是一个hostent的结构。如果函数调用失败,将返回NULL。    struct hostent    {        char    *h_name

edmond999的专栏 1758

差分隐私(Differential Privacy)

差分隐私,拉普拉斯机制和高斯机制的证明

Michale_L的博客 1万+

Linux通过域名获取ip工具

1.nslookup # nslookup www.baidu.com # nslookup www.google.com|grep Address | cut -d " " -f 2 |grep -v Address 2.dig # dig www.baidu.com #dig www.google.com. | grep IN | grep -v "\;"|cut -d . -f 4-7...

Android系统攻城狮 3290

Mamba-UNet:用于医学图像分割的类似UNet的纯视觉Mamba网络

所提出的Mamba-UNet的架构如图2所示,其灵感来源于UNet [24] 和 Swin-UNet [3]。首先将大小为 H × W × 1 的2D灰度图像分割成类似于ViT和VMamba的块 [5,16],然后转换为维度为 H/4 × W/4 × 16 的1D序列。一个初始的线性嵌入层将特征维度调整为任意大小C。这些块令牌随后通过多个VSS块和块合并层进行处理,以创建分层特征。块合并层负责下采样和维度增加,而VSS块则专注于学习特征表示。

AI浩 7549

【异常】Cursor编辑器 “Taking longer than expected...“ 报错全链路排查与解决方案

该提示并非传统的程序崩溃或语法报错,本质是Cursor编辑器的超时预警机制——当核心任务(云端AI推理请求、本地项目索引等)的执行耗时超出系统预设阈值时,触发的用户告知类提示,核心诱因是任务执行全链路出现阻塞或性能瓶颈。以下方案按排查优先级从高到低排序,覆盖从快速急救到根因根治的全流程,可按步骤逐步验证解决。针对大仓库、多依赖项目的索引超时问题,核心是通过规则过滤无效文件,从根源降低索引负载。

奶爸的编程之路,也就一周冷个三天 3101

Gethostbyname() --------用域名或者主机名获取IP地址

头文件: #include  #include      struct hostent *gethostbyname(const char *name);     这个函数的传入值是域名或者主机名,例如"www.google.cn"等等。传出值,是一个hostent的结构。如果函数调用失败,将返回NULL。     struct hostent     {

wyq393562305的专栏 740

OsgEarth —— 笔记6 - 开发阶段加入漫游飞行(附源码)

效果      视频地址   源码 源码下载请私信   代码      代码相关内容:           OSG —— 笔记1 - 指令调用模型           OsgEarth —— 笔记1 - 创建地球的earth文件           OsgEarth —— 笔记2 - Qt窗口加载earth地球(附源码)           OsgEarth —— 笔记3 - 为地

1896

gethostbyname()函数详解

基本概念: gethostname()函数主要作用:用域名或者主机名获取地址,操作系统提供的库函数 函数原型: GETHOSTBYNAME(3) Linux Programmer's Manual GETHOSTBYNAME(3) NAME gethostbyname, gethostbyaddr, sethostent,

daiyudong2020的博客 6万+

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

【Linux C | 网络编程】gethostbyname 函数详解及C语言例子

👉本文主要介绍将域名转换为IP地址的函数gethostbyname,以及提供使用例子。

wkd_007的博客 7565

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Linux--域名解析 gethostbyname()函数

域名解析,gethostbyname()

qq_42425882的博客 2259

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【TCP/IP】IP地址与域名之间的转换 - gethostbyname 和 gethostbyaddr函数

域名系统是互联网的一项服务,它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网

Tsang的博客 4068

Linux-C 根据域名获取IP地址

Linux-C 根据域名获取IP地址 一、简述 记--根据域名获取对应的IP地址。 二、效果 三、代码 /* File:main.c * Desciption:Get ip address from domain name. * Author:Genven_Lian...

Genven_Liang的博客 2796

gethostbyname()与gethostbyaddr(),利用域名获取IP和利用IP获取域名

一、 利用域名获取IP地址 使用下列函数可以通过传递字符串格式的域名获取IP地址 #include<netdb.h> struct hostent * gethostbyname(const char * hostname) :成功时返回hostent结构体地址,失败返回NULL指针 hostent结构体介绍:返回的地址信息装入hostent结构体,此结构体定义如下: struct ...

dustnn的博客 661

linux c 根据域名获取ip,Linux-C 根据域名获取IP地址

Linux-C 根据域名获取IP地址一、简述记--根据域名获取对应的IP地址。二、效果 三、代码/* File:main.c* Desciption:Get ip address from domain name.* Author:Genven_Liang* Date:2020-05-02* Version:V1.0.0.1*/#include #include #include #include...

weixin_29595517的博客 663

如何通过域名获取主机ip地址

它是Linux系统上一个负责ip地址与域名快速解析的文件。hosts文件包含了ip地址与主机名之间的映射,还包括主机的别名。在没有域名解析服务器的情况下,系统上的所有网络程序都通过查询该文件来解析对应于某个主机名的ip地址,否则就需要使用dns服务程序来查找。可以通过将常用的域名和ip地址映射加入到hosts文件中,实现快速方便的访问。系统查询域名的优先级为。的主机数据库文件中保存了一个主机名查询静态表。.除此之外,也可以使用下面的函数在程序中查看映射表。下面是获取主机信息的相关函数,使用时需要包含。

weixin_58004346的博客 1879

Linux下C实现通过域名得到IP

一直想学Linux网络编程 并且固执的要用c来写  认为这样才是最纯正牛x的结果c写网络太难了.....我想实现一个http的简单的get操作居然没有找到一个例子气死我了Linux下的学习资料太少  那 我就来补充吧gethostbyname()真是相当简单 我终于可以自己写一个网络程序了/******************************************************

技术空间 4197

Linux:通过域名,获取IP地址<gethostbyname>函数使用

Linux:通过域名,获取IP地址 参考链接: https://blog.youkuaiyun.com/qq_21794823/article/details/53068351 https://blog.youkuaiyun.com/daiyudong2020/article/details/51946080 源码: #include <stdio.h> #include <string.h&gt...

Linux 1220

linux / gethostbyname() 函数详解

1、作用用域名或者主机名获取地址,操作系统提供的库函数。域名系统(Domain Name System,DNS)主要用于主机名字与 IP 地址之间的映射。每个组织机构往往运行一个或多个名字服务器(name server),我们编写的客户端和服务器等应用程序通过调用解析器(resolver)的函数库中的函数接触 DNS 服务器。常见的解析器函数是 gethostbyname 和 gethostbyaddr,下图展示了应用进程、解析器和名字服务器之间的一个典型关系。 二、返回值 失败返回 NULL 指针,

布袋和尚 3044

gethostbyname()域名解析到IP

入参是域名字符串,返回值中存储了对应的IP地址。除了返回IP信息外,还带有其他信息。域名转IP只需要关注。

随手写下笔记和感悟 535
上一篇: php图片上传与预览
下一篇: 如何在淘宝购物教程
simonjay2007
博客等级 码龄17年 104粉丝 112原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值