Select () 2

Source: Internet
Author: User

Anyone who has been familiar with C/C ++ network programming may know the select Io mode. All online books say fd_set { Int Num; socket arr [ 64  ]} There are some restrictions, because the array length is only 64, you cannot put it if it is more than 64, or you are using multiple threads to use select .. Some books change the definition macro to make the length of the array longer, but it is not practical and cannot be changed dynamically. I cannot always set a very long length. After all, it is on the stack. I was thinking that select can only be used on the client, and the socket cannot exceed 64. Isn't that a weakness ?? I am always confused about this ..... Later, I saw the source code of libevent and found that he also used select .. If someone says this library is easy to process tens of thousands of sockets, I wonder if there is a limit on select ?? How does it ...... I understand the source code. It only uses the handle to store the socket .. Let's take a look at his new definition struct.  Struct  Win_fd_set {u_int fd_count; socket fd_array [  1 ] ;}; This is the new definition struct. A little changed from the original one, but changed 64 to 1. Some people may have seen many such expressions, which are also used in some projects. This method can be dynamically changed using fd_array. Win_fd_set * Set = (win_fd_set *) malloc ( Sizeof (Win_fd_set) + sizoef (scoekt )* 10  ); Set -> Fd_array can have 11 sockets because I have enough memory to have 11 sockets. Remember that there is no data format in the memory, as long as the size is sufficient, whatever you want. The data format is only convenient for us to manage and process data. In this way, the 64-bit size limit is solved .. In fact, I have always wondered why the books in China are the same. libevent has been out for a long time, but no one has ever said this. It may be that the experts did not care about it. Now I gradually like to read open-source code, do not like to read books, and learn how they organize a good project in code. Sometimes I feel: the master uses C to write beautiful C ++ Code, while I am waiting for cainiao to write ugly C code with C ++ ..

 

Select () 2

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.