΢ͻӦ
MICROCOMPUTER & ITS APPLICATIONS
1999 18 2 Vol.18 No.2 1999



WinInetTCP/IPͨ
ףεӨʯ
ժҪWinsockWindowsӦóTCP/IPͨŵı׼ԴΪVisual C++4.2ϰ汾ṩAPIWinInetʹInternet򿪷ÿݶ㡣о̽WinInetʵTCP/IPͨš
ؼʣInternet̡WinsockࡡTCP/IPFTPHTTPGopher
1WindowsϵͳInternet
InternetڵվUNIXʵTCP/IPͨŵı׼Berkeley socketsЭ
Windowsϵͳγ뷢չBerkeley sockets˼룬WindowsӦó֮ʵTCP/IPͨŵı׼Windows Sockets(Winsock)׼
ڴ˻֮ϣֿһЩڳԱտͻ/(Client/Server)ģʽInternet̵APIˣԱɸݲͬҪòͬķʵTCP/IPͨţֱʹWinsockʹϢAPI(Message API)ʹInternetAPI(ISAPI)Visual C++4.2ϰ汾ṩInternet༰WinInetУWinsock(WINSOCK.DLLWSOCK32)Ľӿڣ֤ڲͬWindows(Windows95Windows NT)£۶̬ʵsocketܵĻΣsocketӿͬġWinsockʵTCP/IPͨţ֧Э飬IPX/SPXMAPIṩûѰװĿͻ˷ʹ桢ʼʼϢĽӿ(API)ISPAIǿչHTTPܣĿextensionsfilters:extensionsǿûWebҳϵõĶ̬⣬CGIӦófiltersɷĶ̬⣬ڼӻıݡWinInetṩHTTPFTPGopherӦòЭĿͻ˽ӿڡ
WinInetʹInternet򿪷ÿݶ㡣ĽWinInetԼWinInetInternetͨųнܺ̽֡
2WinInet
2.1ʲôWinInet
WinInetָMicrosoft Win32ṩInternetWININET.DLL̬ЩʹóԱܷʹHTTPFTPgopherInternetԼӱͨܽFingerѯWhoisѯVC 4.2ϰ汾MFCṩWinInet࣬ЩWinSockTCP/IPЭ飬ԱֻЩķ˽Эľݾܱƿͻ˳HTTPFTPgopherվ㡣
2.2WinInet༰书
MFCṩ13WinInet࣬ʵһϵInternetʹܡ
(1)CInternetSession1Internetͨ
(2)CInternetConnection:(ChttpConnectionCftpConnectionCGopherConnection)ӦóInternet(HttpFTPgopher)ӡ
(3)CInternetFile(CHttpFileCGopherFile)ṩ˷Զ̷(Httpgopher)ļϵͳķ
(4)CFileFind:(CftpFileFindCgopherFileFind)ڱؼԶInternetվ(FTP gopher)ļĹܡ
(5)CGopherLocator:gopherվȡgopherλ(locator)ṩCgopherFileFindλ
(6)CInternetException:Internetйص
3WinInetInternetͻ˳
InternetͻӦóָInternetЭ(gopherFTPHTTP)Դ()ȡϢĳ򡣳ԱֱӵWin32MFCWinInetдWinInetͻӦó
ڱInternetͻ˳ĹУWinInetHTTPѯFTPѯgopherѯFingerѯWhoisѯȹģʵ֡ѯҪ跨ӣȻӷֱӽܻӦϢȡԷļϵͳĿƾֱͬЭѯʵ֡
WinInetͷļ"afxinet.h"УʹõWinInetӦóҪ䣺
#include<afxinet.h>
3.1WWW
һHTTPӣ򵥵ķǴCInternetSessionĳһЧHTTPվURLΪúOpenURL()CInternetFileļΪɴURLλwebҳϢļһждȲȡҪϢйس£
CInternetSession session;
CInternetFile*file=NULL;
try {
file=(CInternetFile*)session.OpenURL(URL); }
catch(CInternetException*pEx){
file=NULL;
pEx->Delete(); }
if(file){
//Ҫļд}
trycatch䲶׽ǷURLӶCInternetExceptiontry{}У쳣catch(){}С
WWWվ㣬ʹChttpConnectionࡣ巽ǣhttpִhttpЭ˿ڵĶ˿ں(ȱʡ80)ΪCinternetSession::GetHttpConnection()ĳվӣCHttpConnection::SendRequestHTTPͷ󣬷ֵΪӦϢCHttpFile͵ļ
3.2FTPվ
ȵúCInternetSession::GetFtpConnetion()FTPӣҪĲΪFTPվûûftp˿ں(ȱʡֵΪ21)ģʽ()ûΪգFTPûΪûemailַͨCftpConnectionķԶFTPļϵͳSetCurrentDirectory(GetCurrentDirectory)(ȡ)ӵĵǰFTPĿ¼RemoveDirectory(CreateDirectory)ɾ()Ŀ¼RenameRemovePutFileGetFileOpenFileȶļиɾ롢ȡ򿪵ȲCloseرFTPӡˣMFCṩͼνFTPͻӦó(1)MFCṩĸֿ(static textedit boxButtonList BoxList controlTree control)༭Ի棬ṩõ뼰Ϣû棻(2)MFC ClassWizardмӦûϢӳ꼰(3)FTPӣӦϢӳ亯м룬ʵֶFTPվļϵͳĲϢʾû
FTPӼԶļĹؼ룺
CInternetSession session(My FTP Session);
CFtpConnection* pConn=NULL;
pConn=session.GetFtpConnection(lpSN,lpUN,lpPW,nP);
//lpSN=ftp.whnet.edu.cn,lpUN=lpPW=,np=21,
//Ըվftpʣ

pConn->GetFile(pRF,pLF);
//pRFFTPվȡصļ
//pLFϵͳļ
pConn->GetCurrentDirectory(strCD);
//strCD ΪCstringָ

pConn->Close();session.Close();
3.3Gopherվ
CInternetSession::GetGopherConnection()ĳϷGopherվ㽨ӣΪGopherûûgopherЭ˿ں(ȷʡֵΪ70)ֵΪƴӵCGopherConnectionָ롣
CGopherConnectionĳԱCreateLocator()ڴCGopherLocator3ΪĿ¼ļҹؼ֡ͱ־λֵgopherĳһļĿ¼Ķλ(locator)Ӧó򽫴˶λ͸ļǺţڴgopherϢÿgopherλضȷѲҵļͣCGopherLocatorĳԱGetLocatorType()ɻȡλԡ
ӦóһgopherλΪCGopherFileFindĳԱFindFile()һgopherļļΪĵ2һCGopherFileļô˾ɶдļȡϢɵCGopherFileFindһԱFindNextFile()ͬļCGopherFileFindĴҪԴgopher
gopherļֱʹCGopherConnectionĳԱOpenFile()ҲҪgopherλCGopherFileļ
gopherӼļĹؼ£
CInternetSession session;CGopherConnection* con=NULL;
CGopherFile* pgF=NULL;
con=session.GetGopherConnection(host);
//hostgopher
CGopherFileFind* pF=new CGopherFileFind(con);
CGopherLocator lc=con->CreateLocator(NULL,NULL,
GOPHERTYPEDIRECTORY);
pgF=pF->FindFile(lc,pFN);//pFNΪļַ

3.4GopherFingerѯ
Fingerһֲѯĳһվûĳһָû״ĹInternetЭ顣MFC༰APIоfingerһʣָͨЭ˿ڵķʽgopherѯصWinIntetAPIʵfingerѯ
InternetϢضͶ˿֮Ӧ(IPַ)ȷ˿(port)ȷ̡Эñ׼˿ںš磬ӦóԶ˿ں70gopher(0)ļַ4gopherλ(locator)ʱgopherͻ˽ַ͸øԶĶ˿70Զgopher˿70յѯַgopherЭ涨ӦϢFingerЭı׼˿79ԶϵFinger˶˿ڲFingerӦӦɿͻ˷ַFingerЭ涨ַǡrnӦϢûϢбַǡû+rnӦϢǸûϢ
ˣFingerѯԵóԱCinternetSession::GetGopherConnection()FingerԶ˿79FingerӡڵCGopherConnectionĳԱCreateLocator()λʱԿͻ˷͵FingerѯַΪII(ҹؼ)Fingerվ㷢ѯϢFingerվӦϢCGopherFileļдء
FingerӲFingerѯĹؼ£
CInternetSession sessionCGopherConnection*con=NULL;
con=session.GetGopherConnection(hostName,NULL,NULL,79);
CGopherLocator lc=con->CreateLocator(NULL,qstr,GOPHERTYPETEXTFILE);
//qstrǡrn"û+rn"
CGopherFile* file=con->OpenFile(lc);
//ļ
3.5GopherWhoisѯ
WhoisЭҲWinInetֱ֧֡Finger,WhoisԷ˿ڵַӦWhoisı׼˿43WhoisͨInternetϵDNSϵͳṩڴλʱԴҵΪҹؼ֣ɷصCGopherFileļزѯ
WhoisѯУrs.internic.net"ṩWhoiswww.microsoft.com"Ǳѯ
CInternetSession se;CGopherConnection* con=NULL;
con=se.GetGopherConnection(rs.internic.net",NULL,
NULL,43);
CGopherLocator lc=con->CreateLocator(NULL,
www.microsoft.com",0);
CGopherFile* file=con->OpenFile(lc);
//ļ
WinInetInternetͻ˳ķԷطHTTPFTPGopherFingerWhoisվ㡣 
ߵλ人ѧϢϵ(430074)
ο
1Jamsa KCope K.INTERNET.ӹҵ磬1996
2Kruglinski D J.Visual C++Ļ.廪ѧ磬1994
(ոڣ1998-09-17)
