一 主機數據庫函數
#include <netdb.h>
struct
hostent *gethostbyaddr(
const
void
*addr, //地址
size_t len, //長度
int
type //類型
);
struct
hostent *gethostbyname(
const
char
*name);
這些函數返回的結構中至少包含以下幾個成員
struct
hostent{
char
*h_name;
//
主機名稱
char
**h_aliases;
//
別名列表
int
h_addrtype;
//
地址類型
int
h_length;
//
地址長度
char
**h_addr_list;
//
地址列表
};
如果想獲得某臺計算機的主機數據庫信息,可以調用gethostbyname函數并且將結果打印出來,注意,要把返回的地址列表轉換為正確的地址類型,并用函數inet_ntoa將它們從網絡字節序轉換為可打印的字符串
#include <arpa/inet.h>
char
*inet_ntoa(
struct
in_addr
in
);
函數作用:將一個因特網主機地址轉換為一個點分四元組格式的字符串
#include <unistd.h>
int
gethostname(
char
*name,
int
namelength);
函數作用:將當前主機的名字寫入name指向的字符串中。主機名為null結尾。參數namelength指定了字符串name的長度,如果返回的主機名太長,它就會被截斷
例子:
#include <stdio.h>
#include
<arpa/inet.h>
#include
<stdlib.h>
#include
<netdb.h>
#include
<netinet/
in
.h>
int
main(
int
argc,
char
*
argv[]){
char
*host,**names,**
addrs;
struct
hostent *
hostinfo;
//
把host變量設置為getname程序所提供的命令行參數,或默認設置為用戶主機的主機名
if
(argc==
1
){
char
myname[
256
];
gethostname(myname,
255
);
host
=
myname;
}
else
{
host
=argv[
1
];
}
//
調用gethostbyname,如果未找到相應的信息就報告一條錯誤
hostinfo=
gethostbyname(host);
if
(!
hostinfo){
sprintf(stderr,
"
Cannot get info for host:%s\n
"
,host);
exit(
1
);
}
//
顯示主機名和它可能有的所有別名
printf(
"
result for host:%s\n
"
,host);
printf(
"
Name:%s\n
"
,hostinfo->
h_name);
printf(
"
Aliases:
"
);
names
=hostinfo->
h_aliases;
while
(*
names){
printf(
"
%s
"
,*
names);
names
++
;
}
printf(
"
\n
"
);
if
(hostinfo->h_addrtype!=
AF_INET){
fprintf(stderr,
"
not an IP host!\n
"
);
exit(
1
);
}
addrs
=hostinfo->
h_addr_list;
while
(*
addrs){
printf(
"
%s
"
,inet_ntoa(*(
struct
in_addr*)*
addrs));
addrs
++
;
}
printf(
"
\n
"
);
exit(
0
);
}
?
二 服務信息函數
#include <netdb.h>
struct
servent *getservbyname(
const
char
*name,
//服務名稱
const
char
*
proto
//指定用于連接該服務的協議,它的取值是tcp(用于SOCK_SREAM類型的TCP連接)和udp(用于SOCK_DGRAM類型的UPD數據報)
);
struct
servent *getservbyport(
int
port,
//端口號
const
char
*proto
);
結構servent至少包含一下幾個成員
struct
servent{
char
*s_name;
//
服務名稱
char
**s_aliases;
//
別名列表
int
s_port;
//
IP端口號
char
*s_proto;
//
服務類型
};
例子:
#include <sys/socket.h>
#include
<netinet/
in
.h>
#include
<netdb.h>
#include
<stdio.h>
#include
<unistd.h>
#include
<stdlib.h>
int
main(
int
argc,
char
*
argv[]){
char
*
host;
int
sockfd;
int
len,result;
struct
sockaddr_in address;
struct
hostent *
hostinfo;
struct
servent *
servinfo;
char
buffer[
128
];
if
(argc==
1
){
host
=
"
localhost
"
;
}
else
{
host
=argv[
1
];
}
//
查找主機的地址,如果找不到,就報告一條錯誤
hostinfo=
gethostbyname(host);
if
(!
hostinfo){
fprintf(stderr,
"
no host:%s\n
"
,host);
exit(
1
);
}
//
檢查主機上是否有daytime服務
servinfo=getservbyname(
"
daytime
"
,
"
tcp
"
);
if
(!
servinfo){
fprintf(stderr,
"
no daytime service\n
"
);
exit(
1
);
}
printf(
"
daytime port is %d\n
"
,ntohs(servinfo->
s_port));
//
創建一個套接字
sockfd=socket(AF_INET,SOCK_STREAM,
0
);
//
構造connect調用要使用的地址
address.sin_family=
AF_INET;
address.sin_port
=servinfo->
s_port;
address.sin_addr
=*(
struct
in_addr*)*hostinfo->
h_addr_list;
len
=
sizeof
(address);
//
然后建立連接并取得有關信息
result=connect(sockfd,(
struct
sockaddr *)&
address,len);
if
(result==-
1
){
perror(
"
oops:getdate
"
);
exit(
1
);
}
result
=read(sockfd,buffer,
sizeof
(buffer));
buffer[result]
=
'
\0
'
;
printf(
"
read %d bytes:%s
"
,result,buffer);
close(sockfd);
exit(
0
);
}
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

