基于poll的echo服务器(回显服务器)

代码比较简单,是用C/C++混写的,有疑问可以下方留言


#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <iostream>
#include <signal.h>
#include <vector>
#include <poll.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string>


using namespace std;

#define SERVER_PORT 19999
#define LISTEN_QUEUE_LEN 5

//最大支持客户端数量
#define MAX_CLIENT_NUM  1024

int main(int argc,char**argv)
{
    //忽略SIGPIPE信号
    signal(SIGPIPE, SIG_IGN);

    int fd_server = 0;
    socklen_t num_sock_len = 0;
    sockaddr_in addr_server,addr_client;
    memset(&addr_server,0,sizeof(addr_server));
    memset(&addr_client,0,sizeof(addr_client));
    fd_server = socket(AF_INET,SOCK_STREAM,0);
    if(fd_server < 0)
    {
        cout<<"socket init error!"<<endl;
        exit(1);
    }

    addr_server.sin_family = AF_INET;
    addr_server.sin_addr.s_addr = INADDR_ANY;
    addr_server.sin_port = htons(SERVER_PORT);

    int num_on = 1;
    //端口复用
    setsockopt(fd_server,SOL_SOCKET,SO_REUSEADDR,&num_on,sizeof(num_on));

    if(bind(fd_server,(sockaddr*)&addr_server,sizeof(addr_server))<0)
    {
        cout<<"socket bind error!"<<endl;
        exit(1);
    }
    if(listen(fd_server,LISTEN_QUEUE_LEN)<0)
    {
        cout<<"listen error!"<<endl;
        exit(1);
    }
    int num_fd_size = fd_server + 1;
    vector<pollfd> vec_pollfds;
    vec_pollfds.resize(MAX_CLIENT_NUM);
    vec_pollfds[0].fd = fd_server;
    vec_pollfds[0].events = POLLIN;
    vec_pollfds[0].revents = -1;
    for(int i=1;i<MAX_CLIENT_NUM;++i)
    {
        vec_pollfds[i].fd = -1;
        vec_pollfds[i].events = -1;
        vec_pollfds[i].revents = -1;
    }
    cout<<"wait for client connect..."<<endl;
    int num_poll_ret = 0;
    int fd_client = 0;
    char buff[1024];
    while(1)
    {
        num_poll_ret = poll(&vec_pollfds[0],num_fd_size,-1);
        if(num_poll_ret < 0 )
        {
            cout<<"poll return error!"<<endl;
            exit(1);
        }
        if(0 == num_poll_ret)
        {
            cout<<"poll return 0,continue"<<endl;
            continue;
        }
        //优先处理业务再处理连接
        for(int i=1;i<num_fd_size;++i)
        {
            if(vec_pollfds[i].fd<0) continue;
            if(vec_pollfds[i].revents & POLLIN)
            {
                //重置revents
                vec_pollfds[i].revents = -1;
                //通过fd获取client信息
                getpeername(vec_pollfds[i].fd,(sockaddr*)&addr_client,&num_sock_len);
                memset(buff,0,sizeof(buff));
                //echo 服务器
                std::string str;
                str = string(inet_ntoa(addr_client.sin_addr))+":"+to_string(ntohs(addr_client.sin_port));
                int num_read = read(vec_pollfds[i].fd,buff,sizeof(buff));
                if(0 == num_read)
                {
                    //客户端断开连接
                    cout<<str<<" client close socket!"<<endl;
                    close(vec_pollfds[i].fd);
                    vec_pollfds[i].fd = -1;
                    vec_pollfds[i].events = -1;
                    vec_pollfds[i].revents = -1;
                    continue;
                }
                if(num_read < 0)
                {
                    //出错,demo直接退出
                    cout<<str<<" read error!"<<endl;
                    exit(1);
                }
                //echo
                cout<<"server recv from "<<str<<"  :"<<buff<<endl;
                write(vec_pollfds[i].fd,buff,num_read);
            }
        }

        if(vec_pollfds[0].revents & POLLIN)
        {
            vec_pollfds[0].revents = -1;
            num_sock_len = sizeof(addr_client);
            fd_client = accept(vec_pollfds[0].fd,(sockaddr*)&addr_client,&num_sock_len);
            if(fd_client<0)
            {
                cout<<"accept error!"<<endl;
                continue;
            }
            else
            {
                if(fd_client>= MAX_CLIENT_NUM)
                {
                    //超过最大连接数
                    cout<<"max connect!close!"<<endl;
                    close(fd_client);
                    continue;
                }
                else
                {
                    cout<<"new connect from "<<inet_ntoa(addr_client.sin_addr)<<":"<<ntohs(addr_client.sin_port)<<endl;
                    vec_pollfds[fd_client].fd = fd_client;
                    vec_pollfds[fd_client].events = POLLIN;
                    vec_pollfds[fd_client].revents = -1;
                    if(num_fd_size<fd_client + 1)
                    {
                        num_fd_size = fd_client + 1;
                    }
                }
            }
        }
    }
    return 0;
}

利用了linux的一个特性,文件描述符依次增长一般从3开始,且同一时间具有唯一性,可直接作为数组下标,不像Windows下返回值类似随机。

编译命令

gcc -g *.cpp -o  pollserver -lstdc++

上一篇:力扣LeedCode动态规划专题中等题(八)


下一篇:Java/C++实现代理模式---婚介所