Development of large response scale Winsock applications with completion ports

Source: Internet
Author: User
Tags socket thread

It's not always easy to develop a Web application, but in fact it's just a matter of mastering a few key principles-creating and connecting a socket, trying to connect, and sending and receiving data. The real difficulty is to write a network application that can accept fewer than one, and more than thousands of connections. This article discusses how to develop a highly scalable Winsock application on Windows NT and Windows 2000 through Winsock2. The main focus of the article is on the server side of the client/server model, of course, many of these points apply to both sides of the model.

API and Response Scale

With the overlapping I/O mechanism of WIN32, the application can draw an I/O operation, the overlapping operation request completes in the background, and the thread that draws the operation at the same time to do other things. The thread receives the notification when the overlap operation completes. This mechanism is particularly useful for those time-consuming operations. However, functions such as WSAAsyncSelect () on Windows 3.1 and select () under UNIX are easy to use, but they do not meet the needs of the response scale. The completion port mechanism is optimized for the internal operating system, and on Windows NT and Windows 2000, the overlapping I/O mechanism of the completion port is used to truly enlarge the system's response scale.

Completion port

A completion port is actually a notification queue in which the operating system puts notifications of overlapping I/O requests that have been completed. When an I/O operation completes, a worker thread that can process the result of the operation receives a notification. After a socket is created, it can be associated with a completion port at any time.

Typically, we create a certain number of worker threads in the application to handle these notifications. The number of threads depends on the specific needs of the application. Ideally, the number of threads equals the number of processors, but it also requires that no thread should perform a blocking operation, such as synchronous read and write, waiting for event notifications, to prevent threads from blocking. Each thread is divided into a certain amount of CPU time, during which the thread can run, and then another thread will be divided into a time slice and begin execution. If a thread performs a blocking operation, the operating system deprives it of unused remaining time slices and lets other threads start executing. That is, the previous thread did not fully use its time slices, and when this happens, the application should prepare other threads to make the most of the time slices.

The use of the completion port is divided into two steps. The completion port is created first, as shown in the following code:

HANDLE  hIocp;
hIocp = CreateIoCompletionPort(
  INVALID_HANDLE_VALUE,
  NULL,
   (ULONG_PTR)0,
  0);
if (hIocp == NULL) {
  // Error
}

After the port is created, associate the socket that will use the completion port. The method is to call the CreateIoCompletionPort () function again, the first argument filehandle the handle to the socket, and the second argument Existingcompletionport set to the handle of the completion port that you just created.

The following code creates a socket and associates it with the completion port that was created earlier:SOCKET  s;
s = socket (AF_INET, SOCK_STREAM, 0);
if (s == INVALID_SOCKET) {
  // Error
if (CreateIoCompletionPort((HANDLE)s,
              hIocp,
              (ULONG_PTR)0,
              0) == NULL)
{
// Error
}
...
}

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.