[00015]-[2015-09-04]-[01]-[winsocket Programming 1 Select model development]

Source: Internet
Author: User
Tags set set

The socket Select model is a more commonly used I/O model that enables Windows Sockets applications to be available at the same time
Manages and controls multiple sockets, the core of which is the Select () function----Call the Select () function to check the status of the current multiple sockets
----is readable, writable, abnormal ..... Based on the return value of the function, the readable writable of the socket is judged, and then the corresponding
Windows Sockets API functions to complete the operation of data sending and receiving ....

When the "blocking mode" socket performs an I/O operation, the thread is blocked on the calling function if the condition of the operation is not met.
The program has to be in the waiting state, when the call function returns, it is unknown;

When the "nonblocking mode" socket performs I/O operations, the calling function returns immediately in any case, and the software designer needs to write
More code, the error code returned by the function (Nerrcode) is processed (GetLastError ()), and sometimes in the application
It is often necessary to call the function repeatedly in the body of a loop until the function returns a successful result, which is obviously not a good idea.


"Select () function"
int select (int Nfds, fd_set far* Readfds, Fd_set far* Writefds, fd_set far* exceptfds const struct Timeval far* timeout);

"0" Nfds can be ignored and compatible with the Berkeley socket application
"1" Readfds a pointer to a collection of readable sockets;
"2" Writefds a pointer to a set of writable sockets;
"3" Exceptfds checks the pointer of the wrong socket set;
"4" Timeout set the Select () function wait time


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

struct Timeval
{
Long tv_sec;
Long tv_usec;
};

Timeout = = NULL; Represents an infinite wait until the condition conforms to the return
Timeout = = 0; return immediately
Timeout = * * * * * returns after waiting for * * * time

FD_CLR (S, *set) removes the s socket from the Set collection
Fd_isset (S, *set) checks if S is a member of the set set
Fd_set (S, *set) joins the socket s into the set set
Fd_zero (*set) initializes the set set to an empty collection


Common code structure

SOCKET Listensocket;
Fd_set ALLFD;
Fd_set READFD;
Fd_set WRITEFD;
int reVal;

Fd_zero (&ALLFD);
Fd_set (Listensocket, &ALLFD);

while (1)
{
Fd_zero (READFD);
Fd_zero (WRITEFD);
READFD = ALLFD;
WRITEFD = ALLFD;

ReVal = Select (0, &READFD, &WRITEFD, NULL, NULL);
if (ReVal = = socket_error)
{
Error handling
}
if (ReVal > 0)
{
for (int i = 0; i < Allfd.fd_count; i++)
{
SOCKET sTmp = Allset.fd_array[i];
if (Fd_isset (STMP, &READFD))//readability with data readable or connection initiated
{
if (sTmp = = listensocket)//Because Listensocket continuously listens on the specified port for connection initiation, there is now a link to initiate
{
Create a new connection SOCKET Newacceptsocket = accept (listensocket, NULL, NULL);
// ..........
}
Else
{
have data to accept recvdata ()
// ..........
}
}

if (Fd_isset (STMP, &WRITEFD))
{
SendData ()
// ...........
}
}
}

}

[00015]-[2015-09-04]-[01]-[winsocket Programming 1 Select model development]

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.