Network Programming socket-Overview of TCP select

Source: Internet
Author: User
Tags connect socket

I/O model:

1. Blocking I/O model

2. Non-blocking I/O model: Use fcntl to set the socket to non-blocking, and then round-robin to read data, which will consume a lot of CPU time;

3. I/O multiplexing model: blocking on select;

4. Signal-driven I/O model:

5. asynchronous I/O model: aio_read. The signal is generated only when the data has been copied to the application process buffer.


Select:

The last time parameter of select:

1. If it is null, it indicates that it will wait forever;

2. If the specific time is not 0, it indicates that the return time cannot exceed this time;

3. The second and microsecond values in the struct are both 0, indicating no waiting, that is, polling;


The first parameter of select refers to the number. It is the maximum descriptor to be tested plus 1, because the descriptor starts from 0 and its maximum value is specified by fd_setsize, to increase the descriptor set size, you must first increase the value of fd_setsize and then re-compile the kernel;


Each time the SELECT statement is re-called, all the locations in the descriptor set must be set to 1, because 2, 3, and 4 parameters are value-result parameters, when the function returns, it is changed to a ready descriptor set;


Read now:

1. The data in the socket receiving buffer is greater than or equal to the so_rcvlowat mark size;

2. Receive the TCP connection of Fin; read returns 0;

3. If it is a listener socket, the number of completed Connections cannot be 0;

4. the socket has an error to be processed; read returns-1, and the error code is obtained and cleared through errno or so_error socket options.


Prepare for writing:

1. The available space in the socket sending buffer is greater than or equal to the so_sndlowat mark size;

2. Write of the connection is disabled, and write will generateSigpipeSignal;

3. The non-blocking connect socket has been established or failed;

4. the socket has an error to be processed; write returns-1, and the error code is obtained and cleared through errno or so_error socket options.


Note:

So_rcvlowat for TCP and UDP, the default value is 1;

For TCP and UDP, the default value of sndlowat is 2048;

The integer returned by getsockopt is the socket to be processed error. so_error is then reset from kernel to 0;



Refer:

UNIX Network programmingvolume 1, third edition: thesockets networking API


Network Programming socket-Overview of TCP select

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.