一 主機(jī)數(shù)據(jù)庫(kù)函數(shù)
#include <netdb.h> struct hostent *gethostbyaddr( const void *addr, //地址
size_t len, //長(zhǎng)度
int type //類(lèi)型
); struct hostent *gethostbyname( const char *name);
這些函數(shù)返回的結(jié)構(gòu)中至少包含以下幾個(gè)成員
struct hostent{ char *h_name; // 主機(jī)名稱(chēng) char **h_aliases; // 別名列表 int h_addrtype; // 地址類(lèi)型 int h_length; // 地址長(zhǎng)度 char **h_addr_list; // 地址列表 };
如果想獲得某臺(tái)計(jì)算機(jī)的主機(jī)數(shù)據(jù)庫(kù)信息,可以調(diào)用gethostbyname函數(shù)并且將結(jié)果打印出來(lái),注意,要把返回的地址列表轉(zhuǎn)換為正確的地址類(lèi)型,并用函數(shù)inet_ntoa將它們從網(wǎng)絡(luò)字節(jié)序轉(zhuǎn)換為可打印的字符串
#include <arpa/inet.h> char *inet_ntoa( struct in_addr in );
函數(shù)作用:將一個(gè)因特網(wǎng)主機(jī)地址轉(zhuǎn)換為一個(gè)點(diǎn)分四元組格式的字符串
#include <unistd.h> int gethostname( char *name, int namelength);
函數(shù)作用:將當(dāng)前主機(jī)的名字寫(xiě)入name指向的字符串中。主機(jī)名為null結(jié)尾。參數(shù)namelength指定了字符串name的長(zhǎng)度,如果返回的主機(jī)名太長(zhǎng),它就會(huì)被截?cái)?
例子:
#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變量設(shè)置為getname程序所提供的命令行參數(shù),或默認(rèn)設(shè)置為用戶(hù)主機(jī)的主機(jī)名 if (argc== 1 ){ char myname[ 256 ]; gethostname(myname, 255 ); host = myname; } else { host =argv[ 1 ]; } // 調(diào)用gethostbyname,如果未找到相應(yīng)的信息就報(bào)告一條錯(cuò)誤 hostinfo= gethostbyname(host); if (! hostinfo){ sprintf(stderr, " Cannot get info for host:%s\n " ,host); exit( 1 ); } // 顯示主機(jī)名和它可能有的所有別名 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 ); }
?
二 服務(wù)信息函數(shù)
#include <netdb.h> struct servent *getservbyname( const char *name, //服務(wù)名稱(chēng)
const char * proto //指定用于連接該服務(wù)的協(xié)議,它的取值是tcp(用于SOCK_SREAM類(lèi)型的TCP連接)和udp(用于SOCK_DGRAM類(lèi)型的UPD數(shù)據(jù)報(bào))
); struct servent *getservbyport( int port, //端口號(hào)
const char *proto
);
結(jié)構(gòu)servent至少包含一下幾個(gè)成員
struct servent{ char *s_name; // 服務(wù)名稱(chēng) char **s_aliases; // 別名列表 int s_port; // IP端口號(hào) char *s_proto; // 服務(wù)類(lèi)型 };
例子:
#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 ]; } // 查找主機(jī)的地址,如果找不到,就報(bào)告一條錯(cuò)誤 hostinfo= gethostbyname(host); if (! hostinfo){ fprintf(stderr, " no host:%s\n " ,host); exit( 1 ); } // 檢查主機(jī)上是否有daytime服務(wù) servinfo=getservbyname( " daytime " , " tcp " ); if (! servinfo){ fprintf(stderr, " no daytime service\n " ); exit( 1 ); } printf( " daytime port is %d\n " ,ntohs(servinfo-> s_port)); // 創(chuàng)建一個(gè)套接字 sockfd=socket(AF_INET,SOCK_STREAM, 0 ); // 構(gòu)造connect調(diào)用要使用的地址 address.sin_family= AF_INET; address.sin_port =servinfo-> s_port; address.sin_addr =*( struct in_addr*)*hostinfo-> h_addr_list; len = sizeof (address); // 然后建立連接并取得有關(guān)信息 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 ); }
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
