How to Use SOCKET model overlap I/O

Source: Internet
Author: User

How to Use SOCKET model overlap I/O

 

"As a beginner, I often appreciate the hardships of getting started with beginners, so I always want to take some time to do what I can to help those who need help. I also hope that everyone can share what they have learned with others. Don't despise the greed of others when they ask for it, because what should be despised most is the mean of not giving ."

----- Question by piggyxp (pig)

Preface

In fact, I should apologize first, because in March, I vowed to write a set of Entry Articles and supporting code about All socket models, but I did not expect to be fascinated by the beauty of the day after tomorrow for a holiday, coming back soon ...... -_-B in fact, I have basically finished writing the supporting code for those models, but I didn't write any supporting text. But I 'd like to start with a model that is a little harder, it is easier to get started with other models.

However, because it is also a beginner, we still hope to correct the omissions.

This article embodies the painstaking efforts of the author. If you want to reprint it, please specify the original author and its source. Thank you! Pai_^

OK, let's go! Have fun !! Q ^_^ P

 

Sample source code for this article

(The multi-client MFC Code Compiled by vc.net 2003 is accompanied by detailed comments. It simply shows the characters sent from the client. A slight improvement is a chat room ):

Http://www.haha567.com/PiggyXP/OverlappedModel.rar

(For Unix systems, pay attention to the Case sensitivity of connections)

Thank you very much for the free space provided by the Limin brothers in the online version and the great help and support I provided during my study. Thank you very much ~~~~~ T_t

Welcome to his blog to discuss network technology.

Http://blog.haha567.com/

 

(This article assumes that you have the ability to program with a simple socket model. If you have no knowledge about socket, please pay attention to other articles in this series)

 

Directory:

1. Advantages of overlapping models

2. Basic principles of overlapping models

3. Basic knowledge about overlapping models

4. Implementation steps of overlapping models

5. Considerations for multiple clients

 

 

I. Advantages of overlapping models

1. It can run on all Windows platforms that support Winsock2, and does not support the NT System as the complete port.

2. Compared with blocking, select, wsaasyncselect, and wsaeventselect models, the overlapped I/O model enables applications to achieve better system performance.

Because it is different from the four models, applications using overlapping models notify the buffer sending and receiving system to directly use data, that is, if the application delivers a 10 KB buffer to receive data, and the data has reached the socket, the data will be directly copied to the delivery buffer.

In these four models, the data arrives and is copied to the single-socket receiving buffer. At this time, the application will be notified of the capacity that can be read. After the application calls the receiving function, the data is copied from the single socket buffer to the buffer zone of the application. The difference is shown.

3. from the test results provided in Windows Network Programming, we can see that in the response server that uses the P4 1.7g Xero processor (strong CPU) and MB, A maximum of 40 thousand socket connections can be processed, and the CPU usage is only about 10 thousand when 40% connections are processed-very good performance, which has been forced to complete the port ^_^

Ii. Basic principles of overlapping models

With so many advantages, you must be eager to try it, but we still need to mention the basic principles of the overlapping model first.

To sum up, the overlap model allows an application to use the overlapping data structure (wsaoverlapped) to deliver one or more Winsock I/O requests at a time. After these submitted requests are completed, the application receives a notification, so it can process the data through its own code.

Note that there are two methods to manage the completion of overlapping IO requests (that is to say, a notification is received for the completion of overlapping operations ):

1. event object notification)

2. Completion routines. Note that this is not the completion port.

This article only describes how to use the event notification method to implement the overlapping Io model, and puts the method preparation for completing the routine in the next article. (There is too much content to write ), unless otherwise stated, the overlap model in this article is the event notification-based overlap model by default.

Since it is based on event notification, it is required to associate Windows event objects with the wsaoverlapped structure (wsaoverlapped structure has corresponding parameters). In other words, it is .... By the way, I forgot to mention that since we want to use overlapping structures, our commonly used send, sendto, Recv, and recvfrom will also be replaced by wsasend, wsasendto, wsarecv, and wsarecvfrom, I will talk about their usage later. Here, you only need to note that all their parameters have an overlapped parameter, we can assume that we "bind" our operations such as wsarecv to this overlapping structure, submit a request, and leave other things to the overlapping structure to worry about, the overlapping structure must be "Bound" with the Windows event object, so that we can "Enjoy it" after calling wsarecv. After the overlapping operation is completed, there will naturally be corresponding events to notify us of the completion of the operation, and then we can obtain the desired data based on the results of the overlapping operations.

Maybe you haven't understood it for a long time, so let's continue watching it later ....... -_-B. The language expression capability is limited ~~~

 

 

III. Basic knowledge about overlapping models

The following describes and provides examples of several key functions that will be used in programming overlapping models.

1. wsaoverlapped Structure

This structure is naturally the core of the overlapping model, which is defined in this way.

Typedef struct _ wsaoverlapped {

DWORD internal;

DWORD internalhigh;

DWORD offset;

DWORD offsethigh;

Wsaevent hevent; // the unique parameter to be followed, used to associate the wsaevent object

} Wsaoverlapped, * lpwsaoverlapped;

We need to ship wsarecv and other operations to an overlapping structure, and we need an event object "Bound" with the overlapping structure to notify us of the completion of the operation, you can see the hevent parameter. You should know how to bind the event object to the overlapping structure? It is roughly as follows:

Wsaevent event; // defines an event

Wsaoverlapped acceptoverlapped; // defines the overlapping structure

Event = wsacreateevent (); // create an event object handle

Zeromemory (& acceptoverlapped, sizeof (wsaoverlapped); // initialize the overlapping structure

Acceptoverlapped. hevent = event; // done !!

 

2. wsarecv series Functions

In the overlap model, it depends on the received data, and its parameters are more than the Recv, because it is defined as follows:

Int wsarecv (

Socket S, // Of course, it is the socket for posting this operation

Lpwsabuf lpbuffers, // receives the buffer, which is different from the Recv function.

// Here an array consisting of wsabuf structures is required

DWORD dwbuffercount, // Number of wsabuf structures in the array

Lpdword lpnumberofbytesrecvd, // if the receiving operation is completed immediately, a function call will be returned here

// Number of received bytes

Lpdword lpflags, // The length is long. Set it to 0.

Lpwsaoverlapped lpoverlapped, // overlapping structure of "binding"

Lpwsaoverlapped_completion_routine lpcompletionroutine

// The parameters that will be used in the completion routine. Here we set it to null.

);

Return Value:

Wsa_io_pending: The most common return value. This indicates that the wsarecv operation is successful,

The I/O operation is not completed yet, so we need to bind an event to notify us when the operation will be completed.

 

For example: (the sequence of variable definitions corresponds to the sequence described above, the same below)

Socket S;

Wsabuf databuf; // defines the buffer of the wsabuf Structure

// Initialize databuf

# Define data_bufsize 5096

Char buffer [data_bufsize];

Zeromemory (buffer, data_bufsize );

Databuf. Len = data_bufsize;

Databuf. Buf = buffer;

DWORD dwbuffercount = 1, dwrecvbytes = 0, flags = 0;

// Create the required overlapping structure

Wsaoverlapped acceptoverlapped; // If You Want To process multiple operations, you need one

// Wsaoverlapped Array

Wsaevent event; // if multiple events are required, a wsaevent array is also required.

// Note that a socket may have more than one overlapping request at the same time,

// Corresponds to more than one wsaevent

Event = wsacreateevent ();

Zeromemory (& acceptoverlapped, sizeof (wsaoverlapped ));

Acceptoverlapped. hevent = event; // The key step is to "bind" the event handle to the overlapping structure.

// After doing so much work, we can finally use wsarecv to deliver our requests to the overlapping structure, call ....

Wsarecv (S, & databuf, dwbuffercount, & dwrecvbytes,

& Flags, & acceptoverlapped, null );

I will not describe other functions here, because we have such a good helper as msdn after all, and I will also talk about some other functions when talking about the subsequent completion routine and completion port.

 

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.