In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Native APIselectint select (int numfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, struct timeval * timeout)
Function parameter
Numfds: the maximum value of the file descriptor + 1 (to limit the range of detection file descriptors)
Readfds: contains all file descriptors that are triggered by the select function when the status becomes readable
Writefds: contains all file descriptors that trigger the select function because the state becomes writable
Exceptfds: contains all file descriptors that trigger the select function due to a special exception in the state
Timeout: indicates the blocking timeout period
Return value
A value of-1 indicates an error
A value of 0 indicates a timeout
When greater than 0, it succeeds.
/ / add fd to set FD_SET (int fd, fd_set * set); / / remove fdFD_CLR from set (int fd, fd_set * set); / / determine whether fd is FD_ISSET in set (int fd, fd_set * set); / / clear set entire 0FD _ ZERO (fd_set * set)
The basic idea is to load the file descriptor to be detected into the collection of type fd_set, and then call the select function to detect the file descriptor loaded into the collection
The file descriptors monitored by the select function are divided into three categories, namely writefds, readfds, and exceptfds. After the call, the select function blocks until the file descriptor is ready (data is readable, writable, or except), or timeout (timeout specifies the waiting time, if the immediate return is set to null), the function returns; when the select function returns, you can find the ready descriptor by traversing the fdset.
# include # include const int MAXSIZE = 1024 AF_INET main () {int sockfd =: socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); / / sockfd is the server socket sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons (4567); / / 1024 ~ 49151: the port number sin.sin_addr.s_addr = INADDR_ANY; sockaddr_in client_addr registered by ordinary users / /... bind and listen operations socklen_t clen = sizeof (sockaddr_in); struct timeval tv; int fds [MAXSIZE]; memset (fds,-1,sizeof (fds)); fd_set fdset; fds [0] = sockfd; while (1) {FD_ZERO (& fdset); int I = 0; int fdmax = fds [0]; for (; I)
< MAXSIZE; i++) { if (fds[i] != -1) { FD_SET(fds[i], &fdset); if (fdmax < fds[i]) { fdmax = fds[i]; } } } tv.tv_sec = 2; tv.tv_usec = 0; int res = select(fdmax + 1, &fdset, NULL, NULL, &tv); assert(res != -1); if (res == 0) { printf("timeout\n"); } else { int i = 0; for (; i < MAXSIZE; i++) { if (fds[i] == -1) { continue; } if (FD_ISSET(fds[i], &fdset)) { if (fds[i] == sockfd) { int c = accept(sockfd, (struct sockaddr *)&client_addr, &clen); if (c >= 0) {/ / find an empty set to the new socket for (int k = 0; k
< MAXSIZE; k++) { if (fds[i] == 0) { fds[i] = c; break; } } } } else { char buff[256] = {0}; int n = read(fds[i], buff, 255); if (n >0) {printf ("read:%s\ n", buff); write (fds [I], "OK", 2);} else if (n = = 0) {/ / delete socket fds [I] = 0 }}}
There are some imperfections in this code: using arrays to save sockets, it is recommended that it is better to save linked lists in the form of linked lists.
Advantages: cross-platform
Disadvantages:
There is a maximum limit on the number of file descriptors that a single process can monitor, which is generally 1024 on Linux. This limit can be raised by modifying macro definitions or even recompiling the kernel, but this can also result in reduced efficiency.
To call select every time, you need to copy the fd collection from the user state to the kernel state. In many cases, the fd will be very expensive.
Each call to select requires the kernel to traverse all the fd passed in, and when fd is very expensive, it is also very expensive.
Note that the fdset collection is FD_ZERO (& fdset) before each call to select, that is, emptying.
Reference article
Linux I / O Multiplexing Technology
Pollint poll (struct pollfd * fds, unsigned int nfds, int timesout)
Function parameters:
Represents an array of pollfd structures. Used to save the file descriptor you want to listen to and the corresponding events registered (bound)
Represents the size of the collection of listening events
Specifies the timeout value for poll. When timeout is-1, it blocks until an event occurs; when timeout is 0, it returns immediately.
Return value:
A value of-1 indicates failure, a value of 0 indicates a timeout, and an integer greater than 0 indicates successful execution and the number of file descriptors.
Unlike select, which uses three bitmaps to represent three fdset, poll is implemented using a pointer to a pollfd.
Struct pollfd {int fd; / * file descriptor * / short events; / * requested events to watch * / short revents; / * returned events witnessed * /}
The structure contains the waiting event to monitor and the actual event that occurs.
Frequently detected event tags:
POLLIN/POLLRDNORM: readable
POLLOUT/POLLWRNORM: writable
POLLERR: error
The legal event tags are as follows:
POLLIN: there is data to read
POLLRDNORM: there is ordinary data to read
POLLRDBAND: priority data to read
POLLPRI: there is urgent data to read
POLLOUT: writing data does not cause blocking
POLLWRNORM: writing normal data does not cause blocking
POLLWRBAND: writing priority data does not cause blocking
POLLMSG SIGPOLL: messages are available
POLLIN | POLLPRI is equivalent to the read event of select (), and POLLOUT | POLLWRBAND is equivalent to the write event of select (). POLLIN is equivalent to POLLRDNORM | POLLRDBAND, while POLLOUT is equivalent to POLLWRNORM.
In principle, both select and poll need to get the ready socket by traversing the file descriptor after returning. But unlike select, after calling this function, the system does not have to empty the collection of socket descriptors it detects
Therefore, the select function is suitable for detecting only a small number of socket descriptors, while the poll function is suitable for the case of a large number of socket descriptors.
# include # define OPEN_MAX 100int main (int argc, char * argv []) {/ / 1 Create a tcp listening socket int sockfd =: socket (AF_INET, SOCK_STREAM, 0); / / 2. Bind sockfd struct sockaddr_in my_addr; bzero (& my_addr, sizeof (my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_port = htons (8000); my_addr.sin_addr.s_addr = htonl (INADDR_ANY); bind (sockfd, (struct sockaddr *) & my_addr, sizeof (my_addr)); / / 3. Monitor listen listen (sockfd, 10); / / prepare struct pollfd client [open _ MAX]; int I = 0, maxi = 0; for (; ihandle_write (socket);} void epollserver::handle_accept () {this- > accept ();} void epollserver::handle_read (int socket) {int nread; char buf [Max _ SIZE]; nread =: read (socket, buf, MAX_SIZE) If (nread = = SOCKET_ERROR) {cout close (socket); / / remember close fd delete_event (socket, EPOLLIN); / / remove snooping} else {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.