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

How to use poll in socket programming

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use poll in socket programming". In daily operation, I believe many people have doubts about how to use poll in socket programming. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use poll in socket programming". Next, please follow the editor to study!

one。 About poll

For the IO reuse model, its advantage is undoubtedly that it eliminates the waiting for one IO event to be ready, and instead detects multiple IO data at the same time. When at least one of the waiting events is ready, it will return and tell the user process, "there is data ready, let's see which one is ready to deal with it." for the implementation of IO reuse, in addition to using the select function. Another function that still supports this reuse IO model is the poll function.

two。 The usage of poll function

Although it is also testing and waiting for multiple IO events, poll and select are somewhat different:

In the function parameters

Let's start with nfds, which refers to the number of file descriptors that currently need to be concerned about.

Timeout also sets the timeout, except that unlike select's timeout, which is a structure, it's just an integer type, and it means milliseconds.

Fds is a structure pointer, as follows:

In the structure

Fd represents the file descriptor you care about

Events represents the event that the file descriptor cares about, which is an input parameter that tells the operating system what the action event is concerned about by the event corresponding to the file descriptor, such as read or write.

Revents is an output parameter that tells the user what action event is ready when poll returns. For example, if POLLIN is ready, the value of revent when returned is POLLIN, which tells the user that the POLLIN of the fd event is ready.

Values for events and revents can be as follows:

There are actually more than these three options, but these three options are the most commonly used in the discussion here.

Setting events to POLLIN means that fd needs to read the data, while if revents returns POLLIN, it means that data can be read by ready.

Similarly, events is set to POLLOUT to indicate the writing of data that fd is concerned about, while the return of POLLOUT by revents indicates that the write event is ready for data writing

As for POLLPRI, the later explanation is set as an emergency option. In the TCP protocol message, there is an emergency pointer of URG indicating that it should be read from the place where the emergency data is first read. This is also what it means here.

Unlike select, reads and writes are input and output parameters, which I set in my last blog:

Else if (FDS [I] > 0 & &\ FD_ISSET (fds [I], & reads)) / / normal event, but non-listening time, which represents the newly created new_sock. {/ / char buf [1024]; ssize_t s = read (FDS [I], buf,sizeof (buf)-1) If (s > 0) {buf [s] ='\ 0mm; / / printf ("client:% s\ n", buf) Printf ("client:% s", buf); FD_SET (fds [I], & writes); / / write (FDS [I], buf,sizeof (s) + 1) } else if (s = = 0) {printf ("client quit...\ n"); close (FDS [I]) Fds [I] =-1 } else {} else {} if (FDS [I] > zero &\ FD_ISSET (FDS [I]) & writes) {write (FDS [I], buf,sizeof (buf)) }

The fds [I] is set to the write event when the read event is obtained, while in poll, the read event judgment is modified to the write event. You need to wait for the next loop to get the write event, and then you must set the events property back.

Poll differs from select in that descriptors are stored differently and parameter types are different.

1. Structure array management: every time there is a descriptor of concern, put it in the structure, every time there is an invalid descriptor, set its descriptor to-1, the next time the poll function will ignore it. When a new descriptor is added, traverse the structure from scratch and set the element with a value of-1 to the descriptor event state you want to care about. Remember: update the number of concerned descriptors, the second parameter of poll, when a new descriptor is added to the end of the structure array.

two。 After each call to poll, the structural element revents stores the ready event state, and before each re-call to poll, the system will set it to 0 and re-listen for concerned events (no need for users to reset 0)

The parameters in 3.poll are not input and output, so the timeout,struct pollfd * fds parameter does not need to be reset. Nfds depends (see the first point), while the select function is the input and output type, which needs to be reset before each call.

Let's look at the code on the server side, and the client side will not write it:

# include#include#include#include#include#include#include#include#include#include#define _ BACKLOG_ 5#define _ SIZE_ 64static void usage (const char * proc) {printf ("% s [ip] [prot]\ n", proc);} static int start (char * ip,int port) {int sock = socket (AF_INET,SOCK_STREAM,0); if (sock < 0) {perror ("socket") Exit (1);} int opt = 1; if (setsockopt (sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof (opt)) < 0) {perror ("setsockopt"); exit (3);} 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 cycles &\ Polls [I] .revents = = POLLOUT) {write (Polls [I] .fd, buf,sizeof (buf)) Polls [I] .events = POLLIN;} break } return 0;}

Running result:

At this point, the study on "how to use poll in socket programming" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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