Linux poll服务端

这篇博客介绍了如何在Linux环境下使用poll系统调用实现一个服务端。通过提供的源代码pollServer.cpp,博主展示了编译和运行服务端的过程。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值