分类:
2008-10-13 16:51:17
在学习Winsock网络编程的时候,看到函数getservbyname,文档上说该函数的功能是根据服务的名称获得服务的信息,于是我写了如下的测试程序,结果发现这里所说的获得服务的信息,不能获得我机器上已经开放的服务信息,而只能获得标准的服务,如www,ftp等服务信息。程序如下:
/*根据服务名获取服务的信息,顺便学习Winsock函数getservbyname的用法*/
#include
#include
#include
using namespace std;
int main(int argc, char* argv[]){
if(argc != 2){
cout << "Please input the Server Name." << endl;
exit(0);
}
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2,2);
err = WSAStartup(wVersionRequested, &wsaData);
if(err != 0){
cout << "Can not find a usable Winsock dll." << endl;
return 0;
}
//输出WSADATA中的信息
cout << "输出WSADATA中的信息" << endl;
cout << "wsaData.wVersion:" << (int)LOBYTE(wsaData.wVersion) << "." << (int)HIBYTE(wsaData.wVersion) << endl;
cout << "wsaData.wHighVersion:" << (int)LOBYTE(wsaData.wHighVersion) << "." << (int)HIBYTE(wsaData.wHighVersion) << endl;
cout << "wsaData.szDescription:" << wsaData.szDescription << endl;
cout << "wsaData.szSystemStatus:" << wsaData.szSystemStatus << endl;
if(wsaData.iMaxSockets == 0){
cout << "wsaData.iMaxSockets:" << "Ignore" << endl;
}else{
cout << "wsaData.iMaxSockets:" << wsaData.iMaxSockets << endl;
}
if(wsaData.iMaxUdpDg == 0){
cout << "wsaData.iMaxUdpDg:" << "Ignore" << endl;
}else{
cout << "wsaData.iMaxUdpDg:" << wsaData.iMaxUdpDg << endl;
}
if(wsaData.lpVendorInfo == 0){
cout << "wsaData.lpVendorInf" << "Ignore" << endl;
}else{
cout << "wsaData.lpVendorInf" << wsaData.lpVendorInfo << endl;
}
//调用getservbyname
servent* server_info;
server_info = getservbyname(argv[1], NULL);
//输出server_info
cout << "输出servent中的信息" << endl;
if(server_info != NULL){
cout << "server_info->s_name:" << server_info->s_name << endl;
for(int i=0; server_info->s_aliases[i] != NULL; i++){
cout << "server_info->s_aliases:" << server_info->s_aliases[i] << endl;
}
cout << "server_info->s_port:" <
cout << "server_info->s_prot" << server_info->s_proto << endl;
}
WSACleanup( );
return 0;
}
测试结果如下:
GetServByName www
输出结果如下:
输出WSADATA中的信息
wsaData.wVersion:1.1
wsaData.wHighVersion:2.2
wsaData.szDescription:WinSock 2.0
wsaData.szSystemStatus:Running
wsaData.iMaxSockets:32767
wsaData.iMaxUdpDg:65467
wsaData.lpVendorInf?aA
输出servent中的信息
server_info->s_name:http
server_info->s_aliases:www
server_info->s_aliases:www-http
server_info->s_port:80
server_info->s_prottcp
但是,我的机器上开放了Server服务,而执行
GetServByName Server
却得不到任何信息,输出如下:
输出WSADATA中的信息
wsaData.wVersion:1.1
wsaData.wHighVersion:2.2
wsaData.szDescription:WinSock 2.0
wsaData.szSystemStatus:Running
wsaData.iMaxSockets:32767
wsaData.iMaxUdpDg:65467
wsaData.lpVendorInf?aA
输出servent中的信息
有此可见,调用getservbyname只能得到标准的服务,如www,ftp等的信息,但是对于这些标准的信息,我们完全没有必要调用这个函数,不是吗?
为了进一步观察是否能够得到其它一些服务的信息,我又写了以下的程序,使用getservbyport函数遍历从0到62235的端口,看看能得到哪些服务的信息
/*根据服务端口获取服务的信息,顺便学习Winsock函数getservbyport的用法*/
#include
#include
#include
using namespace std;
int main(int argc, char* argv[]){
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2,2);
err = WSAStartup(wVersionRequested, &wsaData);
if(err != 0){
cout << "Can not find a usable Winsock dll." << endl;
return 0;
}
//调用getservbyport遍历从0到65535的端口
servent* server_info;
for(int i=0;i< 65536;i++){
server_info = getservbyport(i, NULL);
//输出server_info
if(server_info != NULL){
cout << "server_info->s_name:" << server_info->s_name << endl;
for(int i=0; server_info->s_aliases[i] != NULL; i++){
cout << "server_info->s_aliases:" << server_info->s_aliases[i] << endl;
}
cout << "server_info->s_port:" <
cout << "server_info->s_prot" << server_info->s_proto << endl;
cout << endl;
}
}
WSACleanup( );
return 0;
}
执行该程序,输出结果如下:
server_info->s_name:exec
server_info->s_port:512
server_info->s_prottcp
server_info->s_name:login
server_info->s_port:513
server_info->s_prottcp
server_info->s_name:nfsd
server_info->s_aliases:nfs
server_info->s_port:2049
server_info->s_protudp
server_info->s_name:cmd
server_info->s_aliases:shell
server_info->s_port:514
server_info->s_prottcp
server_info->s_name:printer
server_info->s_aliases:spooler
server_info->s_port:515
server_info->s_prottcp
server_info->s_name:talk
server_info->s_port:517
server_info->s_protudp
server_info->s_name:knetd
server_info->s_port:2053
server_info->s_prottcp
server_info->s_name:ntalk
server_info->s_port:518
server_info->s_protudp
server_info->s_name:echo
server_info->s_port:7
server_info->s_prottcp
server_info->s_name:efs
server_info->s_port:520
server_info->s_prottcp
server_info->s_name:discard
server_info->s_aliases:sink
server_info->s_aliases:null
server_info->s_port:9
server_info->s_prottcp
server_info->s_name:systat
server_info->s_aliases:users
server_info->s_port:11
server_info->s_prottcp
server_info->s_name:daytime
server_info->s_port:13
server_info->s_prottcp
server_info->s_name:timed
server_info->s_aliases:timeserver
server_info->s_port:525
server_info->s_protudp
server_info->s_name:tempo
server_info->s_aliases:newdate
server_info->s_port:526
server_info->s_prottcp
server_info->s_name:qotd
server_info->s_aliases:quote
server_info->s_port:17
server_info->s_prottcp
server_info->s_name:courier
server_info->s_aliases:rpc
server_info->s_port:530
server_info->s_prottcp
server_info->s_name:chargen
server_info->s_aliases:ttytst
server_info->s_aliases:source
server_info->s_port:19
server_info->s_prottcp
server_info->s_name:conference
server_info->s_aliases:chat
server_info->s_port:531
server_info->s_prottcp
server_info->s_name:ftp-data
server_info->s_port:20
server_info->s_prottcp
server_info->s_name:netnews
server_info->s_aliases:readnews
server_info->s_port:532
server_info->s_prottcp
server_info->s_name:radius
server_info->s_port:1812
server_info->s_protudp
server_info->s_name:ftp
server_info->s_port:21
server_info->s_prottcp
server_info->s_name:netwall
server_info->s_port:533
server_info->s_protudp
server_info->s_name:radacct
server_info->s_port:1813
server_info->s_protudp
server_info->s_name:telnet
server_info->s_port:23
server_info->s_prottcp
server_info->s_name:smtp
server_info->s_aliases:mail
server_info->s_port:25
server_info->s_prottcp
server_info->s_name:uucp
server_info->s_aliases:uucpd
server_info->s_port:540
server_info->s_prottcp
server_info->s_name:klogin
server_info->s_port:543
server_info->s_prottcp
server_info->s_name:kshell
server_info->s_aliases:krcmd
server_info->s_port:544
server_info->s_prottcp
server_info->s_name:time
server_info->s_aliases:timserver
server_info->s_port:37
server_info->s_prottcp
server_info->s_name:new-rwho
server_info->s_aliases:new-who
server_info->s_port:550
server_info->s_protudp
server_info->s_name:rlp
server_info->s_aliases:resource
server_info->s_port:39
server_info->s_protudp
server_info->s_name:nameserver
server_info->s_aliases:name
server_info->s_port:42
server_info->s_prottcp
server_info->s_name:nicname
server_info->s_aliases:whois
server_info->s_port:43
server_info->s_prottcp
server_info->s_name:remotefs
server_info->s_aliases:rfs
server_info->s_aliases:rfs_server
server_info->s_port:556
server_info->s_prottcp
server_info->s_name:rmonitor
server_info->s_aliases:rmonitord
server_info->s_port:560
server_info->s_protudp
server_info->s_name:monitor
server_info->s_port:561
server_info->s_protudp
server_info->s_name:domain
server_info->s_port:53
server_info->s_prottcp
server_info->s_name:man
server_info->s_port:9535
server_info->s_prottcp
server_info->s_name:bootps
server_info->s_aliases:dhcps
server_info->s_port:67
server_info->s_protudp
server_info->s_name:bootpc
server_info->s_aliases:dhcpc
server_info->s_port:68
server_info->s_protudp
server_info->s_name:tftp
server_info->s_port:69
server_info->s_protudp
server_info->s_name:gopher
server_info->s_port:70
server_info->s_prottcp
server_info->s_name:finger
server_info->s_port:79
server_info->s_prottcp
server_info->s_name:http
server_info->s_aliases:www
server_info->s_aliases:www-http
server_info->s_port:80
server_info->s_prottcp
server_info->s_name:kpop
server_info->s_port:1109
server_info->s_prottcp
server_info->s_name:kerberos
server_info->s_aliases:krb5
server_info->s_aliases:kerberos-sec
server_info->s_port:88
server_info->s_prottcp
server_info->s_name:hostname
server_info->s_aliases:hostnames
server_info->s_port:101
server_info->s_prottcp
server_info->s_name:iso-tsap
server_info->s_port:102
server_info->s_prottcp
server_info->s_name:rtelnet
server_info->s_port:107
server_info->s_prottcp
server_info->s_name:pop2
server_info->s_aliases:postoffice
server_info->s_port:109
server_info->s_prottcp
server_info->s_name:pop3
server_info->s_port:110
server_info->s_prottcp
server_info->s_name:sunrpc
server_info->s_aliases:rpcbind
server_info->s_aliases:portmap
server_info->s_port:111
server_info->s_prottcp
server_info->s_name:auth
server_info->s_aliases:ident
server_info->s_aliases:tap
server_info->s_port:113
server_info->s_prottcp
server_info->s_name:uucp-path
server_info->s_port:117
server_info->s_prottcp
server_info->s_name:nntp
server_info->s_aliases:usenet
server_info->s_port:119
server_info->s_prottcp
server_info->s_name:ntp
server_info->s_port:123
server_info->s_protudp
server_info->s_name:ldaps
server_info->s_aliases:sldap
server_info->s_port:636
server_info->s_prottcp
server_info->s_name:ldap
server_info->s_port:389
server_info->s_prottcp
server_info->s_name:epmap
server_info->s_aliases:loc-srv
server_info->s_port:135
server_info->s_prottcp
server_info->s_name:netbios-ns
server_info->s_aliases:nbname
server_info->s_port:137
server_info->s_prottcp
server_info->s_name:netbios-dgm
server_info->s_aliases:nbdatagram
server_info->s_port:138
server_info->s_protudp
server_info->s_name:netbios-ssn
server_info->s_aliases:nbsession
server_info->s_port:139
server_info->s_prottcp
server_info->s_name:imap
server_info->s_aliases:imap4
server_info->s_port:143
server_info->s_prottcp
server_info->s_name:phone
server_info->s_port:1167
server_info->s_protudp
server_info->s_name:ms-sql-s
server_info->s_port:1433
server_info->s_prottcp
server_info->s_name:doom
server_info->s_port:666
server_info->s_prottcp
server_info->s_name:ms-sql-m
server_info->s_port:1434
server_info->s_prottcp
server_info->s_name:pcmail-srv
server_info->s_port:158
server_info->s_prottcp
server_info->s_name:snmp
server_info->s_port:161
server_info->s_protudp
server_info->s_name:snmptrap
server_info->s_aliases:snmp-trap
server_info->s_port:162
server_info->s_protudp
server_info->s_name:l2tp
server_info->s_port:1701
server_info->s_protudp
server_info->s_name:print-srv
server_info->s_port:170
server_info->s_prottcp
server_info->s_name:bgp
server_info->s_port:179
server_info->s_prottcp
server_info->s_name:https
server_info->s_aliases:MCom
server_info->s_port:443
server_info->s_prottcp
server_info->s_name:pptp
server_info->s_port:1723
server_info->s_prottcp
server_info->s_name:microsoft-ds
server_info->s_port:445
server_info->s_prottcp
server_info->s_name:irc
server_info->s_port:194
server_info->s_prottcp
server_info->s_name:kpasswd
server_info->s_port:464
server_info->s_prottcp
server_info->s_name:ipx
server_info->s_port:213
server_info->s_protudp
server_info->s_name:wins
server_info->s_port:1512
server_info->s_prottcp
server_info->s_name:kerberos-adm
server_info->s_port:749
server_info->s_prottcp
server_info->s_name:kerberos-iv
server_info->s_port:750
server_info->s_protudp
server_info->s_name:isakmp
server_info->s_aliases:ike
server_info->s_port:500
server_info->s_protudp
server_info->s_name:ingreslock
server_info->s_aliases:ingres
server_info->s_port:1524
server_info->s_prottcp