The Select function is used to refer to __ functions

Source: Internet
Author: User
Tags readable

Prototype

int Select (
int Nfds,
fd_set* Readfds,
fd_set* Writefds,
fd_set* Exceptfds,
const struct TIMEVAL* timeout
);

First parameter Nfds

Description under Linux: Specifies the maximum descriptor for the test, which will be tested at 0 to Nfds.

Under Windows: ignored. The Nfds parameter is included a for compatibility with Berkeley sockets. Ignored.

continued to increase in the evening to go back to write

Code is anything

2005.12.14 Additional

I'm a little irresponsible. Now fill up,

That's a complete point from accept.

First of all:
SOCKET sock;
sock= socket (af_inet,sock_stream,0);

struct sockaddr_in addr; Tell sock where to licence.
memset (&addr,0,sizeof (addr));
Addr.sin_family=af_inet;
Addr.sin_port=htons (11111); Port.
Addr.sin_addr.s_addr=htonl (Inaddr_any); Start listening on all IP on this machine

Bind (sock, (SOCKADDR *) &addr,sizeof (addr)),//bind ....

Listen (sock,5); ;//Maximum 5 queues

SOCKET Socka; This is used to accept a connection
Fd_set RfD; Descriptor set this will be used to test if there is an available connection
struct Timeval timeout;

Fd_zero (&RFD); It's always like this to empty a descriptor set first.

timeout.tv_sec=60; Wait for the Select to use this
timeout.tv_usec=0;

U_long ul=1;

Ioctlsocket (Sock,fionbio,&ul); Using non-blocking connections

Now start with Select
Fd_set (SOCK,&RFD); Put the sock in the set of descriptors to test that means putting sock in RfD so that the next time you call Select to test the RFD, you will test sock (because we put the sock into RDF) a descriptor set can contain multiple descriptors that are being tested.
If the first parameter of the select (sock+1,&rfd,0,0, &timeout) ==0)//Select is negligible (this is written to maintain consistency with Linux) The second parameter is put into the read description that needs to be tested descriptor ( That is, if there is a descriptor that can be read, select returns. The third puts a set of write descriptors to be tested, and the fourth is put into an "executable descriptor set" (?? I don't know, either. The fifth parameter is the timeout (if the timeout period is not yet ready for the descriptor, select returns.) ( If NULL, wait until a descriptor set becomes a ready state)
{//This brace is connected to the above, return 0 then exceed the timeout scheduled time

Processing....

}

if (Fd_isset (SOCK,&RFD))
{//There's a descriptor ready.

Socka=accept (sock,0,0); All right, take it.

You also have to Judge Socka is not a valid socket to do ....

--------------------------------------------------------------------------------------------------------------- ----------------

The general case

Let's say you want to determine if two sockets are readable and writable, that's it:

Suppose Socka and sockb are two sockets they are connected and can send and receive data

Fd_set rfd,wfd;//a test to read a test write

Fd_zero (&RFD);

Fd_zero (&WFD);

Fd_set (SOCKA,&RFD)//Put Socka into read descriptor set

Fd_set (SOCKB,&RFD)//Put SOCKB into read descriptor set

Fd_set (SOCKA,&WFD); put Socka into write descriptor set

Fd_set (SOCKB,&WFD); put sockb into write descriptor set

if (Socket_error!=select (0,&rfd,&wfd,0,0))//test These two descriptor sets, never timeout where RfD is only used to test read WFD only to test write

{//No errors

if (Fd_isset (SOCKA,&RFD))//socka readable

{...}

if (Fd_isset (SOCKB,&RFD)//sockb readable

{...}

if (Fd_isset (SOCKA,&WFD)//socka writable

{...}

if (Fd_isset (SOCKB,&WFD)//sockb writable

{...}

}

.............................................................................................

In general, select is like this .... Remember that this select can also be used for timing, which is said to be accurate.

I can only reach this level by describing my ability to be poor. Try to work slowly in the future.

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.