Non-blocking I/O multiplexing-conncet

Source: Internet
Author: User

When a TCP socket is set to nonblocking and thenConnectIs called,ConnectReturns immediately with an errorEINPROGRESSBut the TCP three-way handshake continues. We then check for either a successful or unsuccessful completion of the connection's establishment usingSelect. There are three uses for a nonblockingConnect:

  1. We can overlap other processing with the three-way handshake.ConnectTakes one RTT to complete (Section 2.6) and this can be anywhere from a few milliseconds on a LAN to hundreds of milliseconds or a few seconds on a WAN. there might be other processing we wish to perform during this time.

  2. We can establish multiple connections at the same time using this technique. This has become popular with Web browsers, and we will show an example of this in Section 16.5.

  3. Since we wait for the connection to be established usingSelect, We can specify a time limitSelect, Allowing us to shorten the timeout forConnect. Many implementations have a timeoutConnectThat is between 75 seconds and several minutes. There are times when an application wants a shorter timeout, and using a nonblockingConnectIs one way to accomplish this. Section 14.2 talks about other ways to place timeouts on socket operations.

As simple as the nonblockingConnectSounds, there are other details we must handle:

  • Even though the socket is nonblocking, if the server to which we are connecting is on the same host, the connection is normally established immediately when we callConnect. We must handle this scenario.

  • Berkeley-derived implementations (and POSIX) have the following two rules regardingSelectAnd nonblockingConnects:

  1. When the connection completes successfully, the descriptor becomes writable (p. 531 of TCPv2 ).

  2. When the connection establishment encounters an error, the descriptor becomes both readable and writable (p. 530 of TCPv2 ).

     

    These two rules regardingSelectFall out from our rules in Section 6.3 about the conditions that make a descriptor ready. a tcp socket is writable if there is available space in the send buffer (which will always be the case for a connecting socket since we have not yet written anything to the socket) and the socket is connected (which occurs only when the three-way handshake completes ). A pending error causes a socket to be both readable and writable.

     

There are using portability problems with nonblockingConnectsThat we mention in the examples that follow.

Three methods of non-blocking connect:

1. The three-way handshake process can overlap with other operations.

2. Multiple connections can be established at the same time. This method is common in browsers.

3. select can be used to shorten the connect wait time. The select time-out period of various versions ranges from 75s to several minutes.

 

Other details to consider:

If the reset end and the client are on the same host, the connect function may return immediately (three handshakes have been completed). This scenario must be considered.

The Berkeley-derived and POSIX systems have the following rules when processing select and non-blocking connect:

1. When the connection is established successfully, the descriptor becomes writable;

2. When an error occurs during the connection process, the descriptor becomes readable and writable.

 

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.