当前位置:网站首页>Non reentrant information about gethostbyname()
Non reentrant information about gethostbyname()
2022-04-22 04:23:00 【It's never too old to learn】
Recently, I found that many little friends are learning by themselves Linux The book of network programming , There may be many puzzling doubts in the middle , Some parts of the book are not explained very thoroughly , I happened to have taken this course before , Today, share a problem you have encountered .
First of all, please read 229 page ,8.4.4 function gethostbyname Examples of non reentrant
Let's take a look at this code :
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
struct hostent *ht=NULL;
char host[]="www.sina.com.cn"; /* Inquire about sina Host domain name */
char host1[]="www.sohu.com"; /* Inquire about sohu Host domain name */
char str[30];
struct hostent *ht1=NULL, *ht2=NULL;
ht1 = gethostbyname(host); /* Inquire about "www.sina.com.cn"*/
ht2 = gethostbyname(host1); /* Inquire about "www.sohu.com"*/
int j = 0;
for(j = 0;j<2;j++){
if(j == 0)
ht = ht1; /*sina*/
else
ht =ht2; /*sohu*/
if(ht){
int i = 0;
printf("get the host:%s addr\n",host); /* Original domain name */
printf("name:%s\n",ht->h_name); /* name */
/* Protocol family AF_INET by IPv4 perhaps AF_INET6 by IPv6*/
printf("type:%s\n",ht->h_addrtype==AF_INET?"AF_INET":"AF_INET6");
printf("legnth:%d\n",ht->h_length); /*IP The length of the address */
/* Print IP Address */
for(i=0;;i++){
if(ht->h_addr_list[i] != NULL){ /* No IP End of address array */
printf("IP:%s\n",inet_ntop(ht->h_addrtype,ht->h_addr_list[i],str,30)); /* Print IP Address */
} else{ /* Reach the end */
break; /* sign out for loop */
}
}
/* Print domain name and address */
for(i=0;;i++){/* loop */
if(ht->h_aliases[i] != NULL){ /* The end of the domain name array was not reached */
printf("alias %d:%s\n",i,ht->h_aliases[i]); /* Print domain name */
} else{ /* ending */
break; /* Exit loop */
}
}
}
}
return 0;
}
This is the result of this code , The result of the second line is what my friend asked me the most incomprehensible : Why? ht Of h_name The output is sohu instead of sina.

This involves gethostbyname How to handle functions , Just need to know a few points :
First of all :gethostbyname The result returned when processing data is stored in a static structure variable , We all know static variables , As a local global variable , Initialize only once , in other words , The next time you operate on this variable , Based on the results of this operation
second :ht1 and ht2 This structure pointer is running gethostbyname This function points to the same address ( Above gethostbyname Store the variable address of the returned result )( You can perform partial execution in the compiler to check the change of variable value )
( The expression may not be very accurate , You can understand what I mean or make comments )
After knowing these two points , We can know ,(14 and 15 That's ok ) At the first time gethostbyname After running , The value of the variable that stores the returned result is the original domain name host Domain name of ( namely www.sina.com.cn), here ht1 Also point to the address where the variable is stored , But after the second run , This value has changed to (www.sohu.com), And at this point ht1 and ht2 The address pointed to is the address of this result , So when the second line outputs the result, even at this time ht The assignment is ht1,(25 and 26 That's ok ) The result is still www.sohu.com, You can share your views or put forward your opinions .
版权声明
本文为[It's never too old to learn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210957006146.html
边栏推荐
- iptables使用
- Browser overview local cache cookies, etc
- Mysql中的Decimal类型是什么?
- 【近日力扣 】两数之和+相同的树
- PHP excel import time format conversion
- sqlilabs(25a-26)
- Matplotlib draw 3dbox
- Rsync remote synchronization
- Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
- Using random function to change the random range
猜你喜欢

How to solve the problem that the table association is not displayed when importing SQL from powerdesipowerdesigner

【机器学习】长短时记忆网络(LSTM)

Sumo tutorial - Highway

Introduction to Intel edge software center

Zuo Chengyun - Dachang question brushing class - the minimum number of exchanges of one character on the left and another character on the right

毕设-SSM校园二手书籍销售系统+论文

Tensorflow error: returned a result with an error set solution

LeetCode 63. 不同路径 II

01-Read&Write

JVM简记
随机推荐
Redis 的过期数据会被立马删除么?
05-Aggregation
02-SparkSQL
WebSocket学习
[recent force deduction] verify binary search tree + convert ordered array into binary search tree
06-Datetimes
【Taro开发】-全局自定义导航栏适配消息通知框位置及其他问题(十四)
How does IOT platform realize business configuration center
01-Read&Write
手机软件(App)测试主要有哪些方面?
Shell programming
想转行学测试,我劝你先了解一下这些内容
动态 | 悬镜安全研发管理体系通过CMMI3国际认证
如何在官网查看OracleJDK那个版本是否收费
Sumo tutorial - Highway
Spark installation and use of educoder
软件测试的测试方法你知道多少?
L1-053 electronic (10 points)
[recent force buckle] sum of two numbers + same tree
LeetCode 63. 不同路径 II