Today I have studied the unique I/O port for completing port iocp on Windows, and recorded the difference between it and common socket.

Source: Internet
Author: User

Iocp in windows and epoll in Linux (# epoll I have not studied yet #) are all in the field of Socket network programming. however, it features that thousands of socket connections are managed using a fixed number of threads.

It is equivalent to asynchronous communication. normal sockets are synchronous. Synchronization may require a thread and a socket link. because the number of threads that the system can run is limited, it becomes a bottleneck. iocp/epoll can solve this problem.

First, you need to understand the common socket TCP usage:

Serve end wsastartup, socket, bind listen, accept. Accept is followed by blocked Recv And send. wsacleanup. After accept, the createthread working thread can separately process Recv and send.

After the client wsastartup, socket, and connect. Connect are blocked Recv and sent. wsacleanup.

Iocp is like this:

Server:

 1   Wsastartup  2   Createiocompletionport: port created  3   Getsysteminfo obtains the number of CPU cores, 4   Createthread creates a number of worker threads for the CPU core,  5   Wsasocket,  6   BIND,  7   Listen,  8    //  All in the loop  9   Wsaaccept,  10   Createiocompletionport: Create an accept socketid and bind it to the preceding port,  11  Wsarecv shipping operation (in fact, you must first accept the data, otherwise you will not be able to accept it later. I don't know why ),  12   //  Loop ends  13   Wsacleanup  14   15   16   The working threads are different.  17 Getqueuedcompletionstatus obtains the data and determines whether it is wsarecv or wsasend based on the data.

Here, wsaaccept getqueuecompletionstatus is blocked, or event-driven, where it will wait...

We can see that many APIs starting with WSA are used in iocp, and multiple working threads are created. This is the thread pool, but you do not need to manage them. getqueuecompletionstatus will be automatically managed.

The two most important differences between iocp and common socket:

One is getqueuecompletionstatus, Which is blocked. wsarecv or wsasend is used based on the received data. the common socket is directly blocked in Recv and send. this is not required for iocp. the other is the previous wsaaccept. After receiving a new client connection, you must first perform wsarecv. the subsequent data sent by the client is the above getqueuecompletionstatus.

Iocp is for the server. for clients, it is the same as for common socket clients.

 

 

The examples I have learned are very concise:

Iocp simple command line exampleProgram(Server and 6000socket connection test client)

Http://files.cnblogs.com/ayanmw/testiocp2.zip

 

Attached previousArticleAfter reading the example, let's take a look at the theory:

Http://blog.csdn.net/piggyxp/article/details/6922277

Search for other iocp ports that overlap I/O.

Iocp model analysis:

Http://linux.it219.com/archives/214

Comparison between Windows iocp model and Linux epoll Module:

Http://blog.csdn.net/ljmwork/article/details/7871922

Search: iocp Linux

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.