Use VC ++ and WinSock to communicate with HTTP servers

Source: Internet
Author: User

Use VC ++ and WinSock to communicate with HTTP servers

Ji Hong

1. Introduction
---- Windows Socket API is an open network programming interface in Windows that supports multiple protocols. It includes a set of standard Berkeley socket function calls, and important extensions for Windows. Windows Socket, continuously improved and fully supported by many companies, has become a de facto standard for Windows Network Programming.

---- This article describes a Winsock-based HTTP client application in the context of VC ++ 6.0. In this example, you can use the same principle to implement client applications for other common Internet protocols (such as time, SMTP, POP3, telnet, and FTP.

2. A simple browser
---- I will create a simple browser to illustrate how to use Winsock to implement HTTP client applications based on the HTTP protocol.

---- First, I will create a help function logfile (), which writes the strings passed to it to the disk file. Then create the core function httpclient () in this example (). In this core function, I will connect to the HTTP server through Winsock (in this example, I will use the computer world daily 168.160.224.185 ). After the connection is successful, send the GET command to the HTTP server to download the files in the specified path (/99/Tips. Use the help function logfile () to record the downloaded data to a local disk file.

Char fname [maxpath];
Void 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"); // world computer report
Iresult = connect (S, (struct sockaddr *)
& ADDR, sizeof (ADDR ));
If (socket_error = iresult)
{
// Connection failed
Wsacleanup ();
Return false;
}
Else {
// Connection successful
Strcpy (strsubaddr, "Get/99/TIPS // R/N ");
Strcpy (fname, "index.htm ");
Iresult = Send (S, strsubaddr, strlen (strsubaddr), 0 );

// Download an object
Do {
Strset (strbuffer ,'');
Iresult = Recv (S, strbuffer, sizeof (strbuffer), 0 );
Logfile (strbuffer );
} While (iresult! = 0 );
}

Wsacleanup ();
Return true;
}

Iii. Test
---- Open visual c ++ 6.0, use MFC to create a project test based on the dialog box, 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 "test,

---- The index.htm file for the "http://www.computerworld.com.cn/99/tips/" will be downloaded to your local device.

Iv. Conclusion
---- This should be the basic code for Netscape Navigator, Internet Explorer, and other browsers to implement Internet access. Among these browsers, more than 90% of the Code is mainly used for HTML display and other local processing.

---- If you are interested in the code, you can add code that explains HTML commands based on the Code to correctly display the downloaded HTML file, that is to say, you will have a browser of your own version. Why not try?

(From China's Computer World)
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.