Linux高性能服务器之多路转接(2)---poll模型

本文介绍了一个使用C语言实现的简单TCP服务器程序,该程序利用poll机制来处理多个客户端连接。服务器通过监听指定的IP地址和端口,接受来自客户端的连接请求,并能够同时响应多个客户端的数据读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
#include <netinet/in.h>  
#include <arpa/inet.h>  
#include <unistd.h>  
#include <string.h>   
#include <poll.h>  
#include <sys/socket.h>  
#include <sys/types.h>  
#include <sys/stat.h>  
  
#define POLLFD_SIZE 1024 /* struct pollfd 结构体数组最大上限 */  
  

struct pollfd array_pollfd[POLLFD_SIZE];  
  
/* 结构体成员详情 
struct pollfd  
{ 
    int fd;        // 关心的描述符 
    short events;  // 关心的事件 
    short revents; // 发生的事件 
}; 
*/  
 
int startup(const char* ip, int port)  
{  
    int sock = socket(AF_INET, SOCK_STREAM, 0);  
    if( sock < 0){  
        perror("socket()");  
        exit(1);  
    }  
  
    int opt = 1;  
    setsockopt(sock , SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));  
  
    struct sockaddr_in local;   
    local.sin_addr.s_addr = inet_addr(ip);  
    local.sin_port = htons(port);  
    local.sin_family = AF_INET;  
  
    if(bind(sock, (struct sockaddr *)&local, sizeof(local) ) < 0){  
        perror("bind()");  
        exit(2);  
    }  
  
    if(listen(sock, 5) < 0){  
        perror("listen()");  
        exit(3);  
    }  
  
    return sock;  
}
 
 

void run_poll(int listen_sockfd);  
  

void run_accept(int listen_sock);  
  

void run_action( int index);  
  
int main(int argc, char **argv)  
{  
    if(argc != 3)  
    {  
        printf("usage: %s[local_ip][local_port]", argv[0]);  
        return 1;  
    }  
  
    int listen_sockfd = startup(argv[1], atoi(argv[2]));  
  
    
    array_pollfd[0].fd = listen_sockfd;  
    array_pollfd[0].events = POLLIN;  

    int idx_init = 1;  
    for(; idx_init < POLLFD_SIZE; ++idx_init)  
    {  
        array_pollfd[idx_init].fd = -1;  
    }  
    int timeout = 1000;
  
  
    while(1)  
    {  
        int ret_poll = poll(array_pollfd,  POLLFD_SIZE, timeout);  
  
        if(ret_poll == 0)   
            printf("timeout\n");  
        else if(ret_poll < 0)  
            perror("poll()");  
        else  
        {
            int idx_check = 0;  
            for(idx_check = 0; idx_check < POLLFD_SIZE; ++idx_check)  
            {  
                if(idx_check == 0 && array_pollfd[0].revents & POLLIN)  
                { 
                    run_accept(listen_sockfd);   
                }  
                else if(idx_check != 0)  
                {
                    run_action(idx_check);  
                }  
            }  
        }  
    }
    return 0;  
}  

  
 
void run_action( int index)  
{  
    if(array_pollfd[index].revents & POLLIN)  
    {
        char buf[1024];    
        memset(buf, 0, sizeof(buf));  
        ssize_t s = read(array_pollfd[index].fd, buf, sizeof(buf)-1);  
        if(s > 0)  
        {  
            buf[s-1] = '\0';  
            printf("client say$ %s \n", buf);  
            array_pollfd[index].events = POLLOUT;  
        }  
        else if( s <= 0)  
        {  
            printf("client quit!\n");  
            close(array_pollfd[index].fd);  
            array_pollfd[index].fd = -1;  
        }  
  
    }  
    else if (array_pollfd[index].revents & POLLOUT)  
    {
        const char* msg = "HTTP/1.1 200 OK\r\n\r\n<html><br/><h1>Hello poll!</h1></html>";  
        write(array_pollfd[index].fd, msg, strlen(msg));      
        close(array_pollfd[index].fd);  
        array_pollfd[index].fd = -1;  
    }  
}  
  

void run_accept(int listen_sock)  
{  
    struct sockaddr_in cliaddr;  
    socklen_t clilen = sizeof(cliaddr);  
  
    int new_sock = accept(listen_sock, (struct sockaddr*)&cliaddr, &clilen);  
    if( new_sock < 0)  
    {  
        perror("accept");  
        return ;  
    }  
  
    printf("与客户端连接成功: ip %s port %d \n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));  

    int idx_find = 1;  
    for(; idx_find < POLLFD_SIZE; ++idx_find)  
    {  
        if(array_pollfd[idx_find].fd < 0)  
        {  
            array_pollfd[idx_find].fd = new_sock;  
            array_pollfd[idx_find].events = POLLIN ;  
            break;  
        }  
    }     
    if(idx_find == POLLFD_SIZE)  
    {  
        perror("连接超出最大限度,add array_pollfd[]");  
        return;  
    }  
  
}  
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值