A simple browser
Beijing Haidian District Zhongguancun Ji Hong
This article will introduce a WINSOCK-based HTTP customer in the VC 5.0 environment.
application. Readers can implement other Internet common protocols based on this example.
(Time, SMTP, POP3, Telnet, FTP, etc.) of the client application.
A simple browser
The author will create a simple browser to explain how to pass Winsock http
The protocol implements HTTP client applications.
First, a help function logfile () will be created, and the function passes the character passed to it.
Write in the disk file. Then create the core function of this example - httpclient
(). In this core function, it will be connected to the HTTP server via WinSo CK (
In this example, "Computer World" Daily URL: 168.160.224.185). Be connected
After the function, send the get command to the HTTP server to download the specified path (/ 99 / tips /)
document. Record the downloaded data to local disks by helping function logfile ()
Part in the part.
Char fname [maxpath]; voi d logfile (char * p)
{
FILE * fp = fopen (FNAME, "A ");
FPRINTF (FP, "% S / N", P);
Fclose (fp);
}
Bool HttpClient (Void)
{
WSADATA WS;
Socket S;
Struct SockAddr_in Addr;
INT IRESULT;
Long LRESULT;
CHAR STRSUBADDR [100], Strbuffer [100];
LRESULT = WSASTARTUP (0x0101, & WS);
S = Socket (AF_INET, SOCK_STREAM, 0);
Addr.sin_Family = AF_INET;
Addr.sin_Port = HTONS (80);
Addr.sin_addr.s_addr = inet_addr ("168.160.224.185"); // Computer World Daily
IRESULT = Connect (S, Struct SockAddr *) & Addr, SizeOf (AddR));
IF (socket_error == IRESULT)
{
// Connection failed
WSACLEANUP ();
Return False;
}
Else {
// connection succeeded
STRCPY (strsubaddr, "get / 99 / tips / / / / / n");
STRCPY (FNAME, "INDEX.HTM");
IRESULT = Send (S, STRSUBADDR, STRLEN (STRSUBADDR), 0);
// download file
Do {
StRSET (strbuffer, '');
IResult = RECV (S, Strbuffer, Sizeof (strBuffer), 0);
Logfile (strbuffer);
} while (IResult! = 0);
}
WSACLEANUP ();
Return True;
}
test
Open Visual C 5.0, create a dialog-based project TEST with MFC, and
Place the TEST button on the dialog box and add the test code.
Void ctestdlg :: ontest ()
{
HttpClient ();
}
Compile and run the test program, in the Test Dialog box, click the Test button, "HT
TP: //www.compute rworld.com.cn/99/tips/ "The index.htm file will be
Download to the local.
in conclusion
This should be the basic code for Internet, Internet Explorer, and other browsers to implement Internet That. In these browsers, other percentages
The code is mainly used for HTML display, etc.
If you are interested in interest, based on these code, add some code to explain the HTML command.
, You can display the downloaded HTML files correctly, that is, you will have a percent.
Hundreds of browsers are available.