Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Ipaw O Multiplex transfer & # 160;-& # 160; poll

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

1. Poll

The implementation of poll is very similar to select, except that the fd collection is described in a different way. Poll uses the pollfd structure instead of select's fd_set structure, and everything else is similar.

2. Poll correlation function

# include

Int poll (struct pollfd * fds, nfds_t nfds, int timeout)

/ / fds: pollfd structure

Events: events to monitor

Revents: events that have occurred, set flags to reflect the existence of relevant conditions

Constant description

POLLIN normal or priority with readable data

POLLRDNORM ordinary data is readable

POLLRDBAND priority with readable data

POLLPRI high priority data readable

POLLOUT ordinary data is writable

POLLWRNORM ordinary data is writable

POLLWRBAND priority with writable data

/ / can only be stored in revents as the returned result of a description word

An error occurred in POLLERR

POLLHUP occurs and hangs

The POLLNVAL description word is not an open file

Struct pollfd {int fd; / * File descriptor * / short events; / * requested event * / short revents; / * returned event * /}

/ / nfds: the number of descriptors to monitor

/ / timeout: specifies how long poll should wait when it does not receive an event before returning [unit: ms].

INFTIM: never time out

0: return immediately

> 0: wait for the specified time

2. Features:

There is no maximum number of pollfd (but performance will degrade if the number is too large).

Like the select function, when poll returns, you need to poll pollfd to get the ready descriptor.

Both select and poll need to get the ready socket by traversing the file descriptor after the return. In fact, a large number of clients connected at the same time may only be in a ready state at a time, so their efficiency decreases linearly as the number of monitored descriptors increases.

3. Poll server

# include#include#include#include#include#include#include#include#include#include# include#define _ BACKLOG_ 5#define _ MAX_NUM_ 64void usage (const char * proc) {printf ("Usage:% s [ip] [port]\ n", proc);} static int startup (const char * ip,const int port) {int sock=socket (AF_INET,SOCK_STREAM,0); if (sock < 0) {perror ("socket") Exit (1);} int opt=1; setsockopt (sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof (opt)); struct sockaddr_in local; local.sin_family=AF_INET; local.sin_port=htons (port); local.sin_addr.s_addr=inet_addr (ip) If (bind (sock, (struct sockaddr*) & local,sizeof (local)) < 0) {perror ("bind"); exit (2);} if (listen (sock,_BACKLOG_) < 0) {perror ("listen"); exit (3) } return sock;} static int poll_server (int listen_sock) {struct sockaddr_in client; socklen_t len=sizeof (client); struct pollfd fds [_ MAX_NUM_]; fds [0] .fd = listen_sock; fds [0] .events = POLLIN; fds [0] .revents = 0; size_t iTuno; for (; I

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.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report