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

The poll function and simple server client programming of Linux advanced Ipaw O

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

When you need to listen to multiple file descriptors at the same time, you need to use the Imax O reuse function, which includes select, poll, and epoll. Today, you mainly use the poll function.

Poll () accepts a pointer to the structure 'struct pollfd' list, which includes the file descriptors and events you want to test. The event is determined by a bit mask of the event domain in the structure. The current structure will be populated after the call and returned after the event.

Function prototype:

# includeint poll (struct pollfd * fds, nfds_t nfds, int timeout); struct pollfd {int fd; / * file descriptor*/ short events; / * requested events*/ short revents; / * returned events*/}

Function argument: fds is the array of fd to listen for, nfds is the number of arrays, and timeout timeout-1 is blocking

Function: use the type of events passed in to determine whether the returned type is consistent, and if so, it's time to do so.

Events:

Constant

Description

POLLIN

Normal or priority with readable data

POLLRDNORM

Ordinary data is readable

POLLRDBAND

Priority band data readable

POLLPRI

High priority data readable

POLLOUT

Ordinary data is writable

POLLWRNORM

Ordinary data is writable

POLLWRBAND

Priority band data writable

POLLERR

An error occurred

POLLHUP

Occurrence of hang

POLLNVAL

The description word is not an open file

Next is an example of a server listening on two socket:

Server code:

# include#include#include# include#define MAX_POLLFD_NUM 2//#define SERVER_CONN_IP1 "1.1.1.1" / / # define SERVER_CONN_IP2 "1.1.1.1" # define DEFAULT_PORT 8000#define BUFF_MAX 1024typedef void (* Server_Rrocess_Thread_Fun) (void * arg) / * thread process function define * / typedef struct sever_thread_fun {Server_Rrocess_Thread_Fun Server_Process_Client_Conn1; Server_Rrocess_Thread_Fun Server_Process_Client_Conn2;} Poll_Server_Process_Clinet_FUN_t;/*connect 1 thread function*/void * Poll_Conn1_Process (void * arg) {unsigned char ucBufArr [BUFF _ MAX] = {0}; unsigned long ulSize = 0; int connect_fd Printf ("come pthread conn1 fun to proess conn1\ n"); if ((connect_fd = accept (* (int*) arg, (struct sockaddr*) NULL, NULL)) =-1) {printf ("accept socket error:% s (errno:% d)", strerror (errno), errno);} ulSize = recv (connect_fd, ucBufArr, BUFF_MAX, 0) If (send (connect_fd, "Hello,you are connected 1!\ n", 26J 0) =-1) perror ("send error"); ucBufArr [ulSize] ='\ 0mm; / / sleep (20); printf ("recv msg from client:% s\ n", ucBufArr); close (connect_fd); pthread_exit ((void*) 1) } / * connect 1 thread function*/void * Poll_Conn2_Process (void * arg) {unsigned char ucBufArr [BUFF _ MAX] = {0}; unsigned long ulSize = 0; int connect_fd; printf ("come pthread conn2 fun to proess conn2\ n") If ((connect_fd = accept (* (int*) arg, (struct sockaddr*) NULL, NULL)) =-1) {printf ("accept socket error:% s (errno:% d)", strerror (errno), errno);} ulSize = recv (connect_fd, ucBufArr, BUFF_MAX, 0) If (send (connect_fd, "Hello,you are connected 2!\ n", 26J 0) =-1) perror ("send error"); ucBufArr [ulSize] ='\ 0mm; / / sleep (20); printf ("recv msg from client:% s\ n", ucBufArr); close (connect_fd); pthread_exit ((void*) 2) } int main () {int poll_ret = 0; pthread_t thread_conn1; pthread_t thread_conn2; / * socket var*/ int server_socket_fd_conn1; int server_socket_fd_conn2; struct sockaddr_in servaddr_conn1; struct sockaddr_in serveraddr_conn2; / * poll var*/ struct pollfd pollfd_ arr [Max _ POLLFD_NUM] / * init thread fun*/ Poll_Server_Process_Clinet_FUN_t server_conn_handel; server_conn_handel.Server_Process_Client_Conn1 = Poll_Conn1_Process; server_conn_handel.Server_Process_Client_Conn2 = Poll_Conn2_Process / * create two server socket*/ if ((server_socket_fd_conn1 = socket (AF_INET, SOCK_STREAM, 0)) =-1) {printf ("create socket conn1 error:% s (errno:% d)\ n", strerror (errno), errno); exit (0) } if ((server_socket_fd_conn2 = socket (AF_INET, SOCK_STREAM, 0)) =-1) {printf ("create socket conn2 error:% s (errno:% d)\ n", strerror (errno), errno); exit (0);} / * init socket 1 for conn1*/ memset (& servaddr_conn1, 0, sizeof (servaddr_conn1)) Servaddr_conn1.sin_family = AF_INET; servaddr_conn1.sin_addr.s_addr = htonl (INADDR_ANY); servaddr_conn1.sin_port = htons (DEFAULT_PORT); / / if (inet_pton (AF_INET, SERVER_CONN_IP1, & servaddr_conn1.sin_addr.s_addr)

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

Servers

Wechat

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

12
Report