Differences between synchronous and asynchronous wininet Development

Source: Internet
Author: User
Tags microsoft website

The concept of synchronization and Asynchronization is not allowed here.
Taking downloading an object as an example, let's take a look at the synchronization practices:
1) internetopen;
2) internetopenurl;
3) httpqueryinfo;
4) internetreadfile;
5) internetclosehandle.
In steps 2 and 3, the program waits until the function returns. To set timeout, you can use internetsetoption (but it seems useless ). In many cases, this function is not suitable. For example, the user takes the initiative to interrupt the download, but can only wait for the function to return. In addition, if a large file is downloaded, you cannot imagine reading the data in the upper MB at a time. You need to use resumable data transfer. Although you can also use the synchronization function internetsetfilepointer to locate the location where the network file is read, however, many servers do not. If you use it on your mobile phone, consider restrictions such as mobile gateway.
The advantage of synchronization is that there are few functions, clear procedures, and convenient debugging.

Let's take a look at asynchronous practices.
1) internetopen, which must be specified as asynchronous;
2) internetsetstatuscallback and set the callback;
3) internetopenurl. You must specify the callback parameter;
4) waitforsingobject or waitformultipleobjects, receiving semaphores;
5) httpqueryinfo;
6) internetreadfileex. You must specify the callback parameter;
7) waitforsingobject or waitformultipleobjects, receiving semaphores;
8) internetsetstatuscallback: uninstall the callback;
9) internetclosehandle.
It can be seen that Asynchronization is much more complicated than synchronization, with the focus on callback functions. In the callback, the system will return various system-defined HTTP messages in a timely manner. We can set certain semaphores based on these messages. In waitforsingobject or waitformultipleobjects, wait for these signals (of course, you can also wait for the user's cancellation action ). When a correct signal is returned, continue.
In asynchronous mode, internetopenurl can set the range to be read in the header. For example, to read data from 0 to 1024, add range: bytes = 0-1024/R/N to the header. This method ensures resumable upload. Note that if the server supports resumable data transfer, the status code obtained using httpqueryinfo is 206 instead of 200.

How do I write callback functions? Let's look at an example provided by Microsoft. In this example, data is uploaded using post, which is more troublesome than the previous steps for downloading data. The internetopenurl function is divided into more functions for processing. You have time to dig this example. The Microsoft example is in a sendreqexasync. cpp file, which should be available on the Microsoft website.

Let's just look at the method of callback:
Void _ stdcall callback (hinternet,
DWORD dwcontext,
DWORD dwinternetstatus,
Lpvoid lpstatusinfo,
DWORD dwstatusinfolen)
Cout <"Callback dwinternetstatus:" <dwinternetstatus <"context:" <dwcontext <Endl;
Cout. Flush ();

Switch (dwcontext)
Case 1: // connection handle
If (dwinternetstatus = internet_status_handle_created)
Internet_async_result * pres = (internet_async_result *) lpstatusinfo;
Hconnect = (hinternet) pres-> dwresult;
Cout <"Connect handle created" <Endl;
Cout. Flush ();
Setevent (hconnectedevent );

Case 2: // request handle
Switch (dwinternetstatus)
Case internet_status_handle_created:
Internet_async_result * pres = (internet_async_result *) lpstatusinfo;
Hrequest = (hinternet) pres-> dwresult;
Cout <"request handle created" <Endl;
Cout. Flush ();

Case internet_status_request_sent:
DWORD * lpbytessent = (DWORD *) lpstatusinfo;
Cout <"Bytes Sent:" <* lpbytessent <Endl;
Dwnumbytescomplete + = * lpbytessent;

Case internet_status_request_complete:
Internet_async_result * pasyncres = (internet_async_result *) lpstatusinfo;
Cout <"function call finished" <Endl;
Cout <"dwresult:" <pasyncres-> dwresult <Endl;
Cout <"dwerror:" <pasyncres-> dwerror <Endl;
Cout. Flush ();
Setevent (hrequestcompleteevent );

Case internet_status_receiving_response:
Cout <"processing ing response" <Endl;
Cout. Flush ();

Case internet_status_response_inclued:
DWORD * dwbytesreceived = (DWORD *) lpstatusinfo;
Cout <"received" <* dwbytesreceived <Endl;
Cout. Flush ();


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.