Tutorials to .com

Tutorials to .com » Software » Vc » Using VC + + and Winsock to achieve with the HTTP server calls

Using VC + + and Winsock to achieve with the HTTP server calls

Print View , by: iSee ,Total views: 27 ,Word Count: 659 ,Date: Wed, 26 Aug 2009 Time: 10:18 AM

Authors: Ji Hong

1 Introduction
---- Windows Socket API is a set of open, multi-protocol network programming under the Windows interface, which includes a standard Berkeley
Socket set of function calls, as well as the important expansion of Windows. Windows Socket Through continuous improvement in public
Many companies with the full support of the Windows network programming has become the de facto standard.

---- This will be vc + + 5.0 environment, describes a Winsock-based HTTP client application. Readers can be based on this case
The same principle the achievement of other commonly used Internet protocols (time, SMTP, POP3, Telnet, FTP, etc.) client application.

Second, a simple browser
---- I will create a simple browser in order to illustrate how Winsock based on the HTTP protocol implementation HTTP client application.

---- First, I will create a helper function LogFile (), the function to the string passed to it written to disk file. And then create the core function of the sample --
HttpClient (). In this core function, I will Winsock to connect to the HTTP server (in this case I will use the computer the World Journal When the connection is successful, send the Get command to the HTTP server to download the designated path
On (/ 99/tips /) file. By helping function LogFile () to download data records 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)
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
( ""); / / Computer World Daily
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 file
do (
strset (strBuffer, '');
iResult = recv (s, strBuffer, sizeof (strBuffer), 0);
LogFile (strBuffer);
) While (iResult! = 0);

WSACleanup ();
return TRUE;

3, test
---- Open the Visual c + + 5.0, with MFC dialog-based project to create Test, and in the dialog box put "Test" button,
And then add the test code.

Void CTestDlg:: OnTest ()
HttpClient ();

---- Compile and run the test program, in the test dialog box, click the "Test" button,

---- "Http://www.computerworld.com.cn/99/tips/" the index.htm file will be downloaded to the local.

4. Conclusion
---- This should be is Netscape Navigator, Internet Explorer and other browsers to achieve Internet visit
To ask the basic code. In these browsers, the other 90% or more displays HTML code that is mainly used for local treatment.

---- If you are interested reader, based on these codes, and then add some code to explain HTML commands, you can correctly display the downloaded HTML document,
That is to say will have its own version of a one hundred percent of the browser. Why not try?

Visual C++ Tutorial Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.