Linux poll服务端
源代码:pollServer.cpp
/*******************************************************************************
* pollServer.cpp
*
* Create on 2018-10-6
* Author: yanxinchun
*
* g++ -o pollServer pollServer.cpp
*******************************************************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/poll.h>
#define POLL_MAX_CN (1024)
#define ERR_EXIT(m) \
do { \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
typedef struct _packet_st {
int len;
char buf[1024];
} PACKET_st;
ssize_t readn(int fd, void *buf, size_t count)
{
size_t nleft = count;
ssize_t nread;
char *bufp = (char*)buf;
while(nleft > 0){
if(nread = read(fd, bufp, nleft) < 0) {
if(errno == EINTR) {
continue;
}
return -1;
}else if(nread == 0) {
return count - nleft;
}
bufp += nread;
nleft -= nrea