[00020]-[2015-09-18]-[00]-[windows Socket Select model]

Source: Internet
Author: User

int Select (
int Nfds;
Fd_set far* Readfds;
Fd_set far* Writefds;
Fd_set far* Exceptfds;
const struct Timeval far* timeout;
);


#define FD_SETSIZE 64
typedef struct FD_SET
{
U_int Fd_count;
SOCKET Fd_array[fd_setsize];
}fd_set;

struct Timeval
{
Long tv_sec;
Long tv_usec;
};

FD_CLR ();//delete s socket from set set
Fd_set ();//Add the S socket to the Set collection
Fd_isset ();//Determines whether the s socket is set in set
Fd_zero ();//Initialize the set set to an empty collection


ThreadFunc (void* pparam)
{
SOCKET Slisten = ...;
Fd_set ALLFD;
Fd_set READFD;
Fd_set WRITEFD;
Fd_zero (&ALLFD);
Fd_set (Slisten, ALLFD);

while (isrunning)
{
Fd_zero (&READFD);
Fd_zero (&WRITEFD);
READFD = ALLFD;
WRITEFD = ALLFD;
int nret = SELECT (0, &READFD, &WRITEFD, NULL, NULL);

if (Nret > 0)
{
for (int i = 0; i < Allfd.fd_count; i++)
{
if (Fd_isset (Allfd.fd_array[i], &READFD))
{
if (allfd.fd_array[i] = = Slisten)
{
Accept ()
Addclient ()
Fd_set (sclient, &ALLFD);
}
else//receiving data
{
Pclient = Getclient (Allfd.fd_array[i]);
Pclient->recvdata ();
}
}

if (Fd_isset (Allfd.fd_array[i], &WRITEFD))
{
Pclient = Getclient (Allfd.fd_array[i]);
Pclient->senddata ()
}

}
}
}
}

[00020]-[2015-09-18]-[00]-[windows Socket Select model]

Related Article

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.