LINUX下完整的TCP epoll 伺服器和客戶端程式碼,用作備份

啊羅羅發表於2020-11-13

 epoll伺服器

#include <iostream>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

using namespace std;

#define MAXLINE 128
#define OPEN_MAX 100
#define LISTENQ 20
#define SERV_PORT 5000
#define INFTIM 1000

void setnonblocking(int sock)
{
    int opts;
    opts = fcntl(sock, F_GETFL);
    if (opts < 0)
    {
        perror("fcntl(sock,GETFL)");
        exit(1);
    }
    opts = opts | O_NONBLOCK;
    if (fcntl(sock, F_SETFL, opts) < 0)
    {
        perror("fcntl(sock,SETFL,opts)");
        exit(1);
    }
}

int main(int argc, char *argv[])
{
    int i, maxi, listenfd, connfd, sockfd, epfd, nfds, portnumber;
    ssize_t n;
    char line[MAXLINE];
    socklen_t clilen;
    portnumber = 5555;

    //宣告epoll_event結構體的變數,ev用於註冊事件,陣列用於回傳要處理的事件

    struct epoll_event ev, events[20];
    //生成用於處理accept的epoll專用的檔案描述符

    epfd = epoll_create(256);
    struct sockaddr_in clientaddr;
    struct sockaddr_in serveraddr;
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    //把socket設定為非阻塞方式

    //setnonblocking(listenfd);

    //設定與要處理的事件相關的檔案描述符

    ev.data.fd = listenfd;
    //設定要處理的事件型別

    ev.events = EPOLLIN | EPOLLET;
    //ev.events=EPOLLIN;

    //註冊epoll事件

    epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &ev);

    serveraddr.sin_family = AF_INET;
    char *local_addr = "127.0.0.1";
    inet_aton(local_addr, &(serveraddr.sin_addr)); //htons(portnumber);

    serveraddr.sin_port = htons(portnumber);
    bind(listenfd, (sockaddr *)&serveraddr, sizeof(serveraddr));
    listen(listenfd, LISTENQ);
    maxi = 0;
    for (;;)
    {
        //等待epoll事件的發生

        nfds = epoll_wait(epfd, events, 20, 500);
        //處理所發生的所有事件

        for (i = 0; i < nfds; ++i)
        {
            if (events[i].data.fd == listenfd) //如果新監測到一個SOCKET使用者連線到了繫結的SOCKET埠,建立新的連線。
            {
                connfd = accept(listenfd, (sockaddr *)&clientaddr, &clilen);
                if (connfd < 0)
                {
                    perror("connfd<0");
                    exit(1);
                }
                //setnonblocking(connfd);
                printf("conn fd :%ul", connfd);
                char *str = inet_ntoa(clientaddr.sin_addr);
                cout << "accapt a connection from " << str << endl;
                //設定用於讀操作的檔案描述符

                ev.data.fd = connfd;
                //設定用於注測的讀操作事件

                ev.events = EPOLLIN | EPOLLET;
                //ev.events=EPOLLIN;

                //註冊ev

                epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &ev);
            }
            else if (events[i].events & EPOLLIN) //如果是已經連線的使用者,並且收到資料,那麼進行讀入。
            {
               // cout << "EPOLLIN" << endl;
                if ((sockfd = events[i].data.fd) < 0)
                    continue;
                if ((n = read(sockfd, line, MAXLINE)) < 0)
                {
                    if (errno == ECONNRESET)
                    {
                        close(sockfd);
                        events[i].data.fd = -1;
                    }
                    else
                        std::cout << "readline error" << std::endl;
                }
                else if (n == 0)
                {
                    close(sockfd);
                    events[i].data.fd = -1;
                }
                line[n] = '0';
               // cout << "read " << line << endl;
                //設定用於寫操作的檔案描述符

                ev.data.fd = sockfd;
                //設定用於注測的寫操作事件

                ev.events = EPOLLOUT | EPOLLET;
                //修改sockfd上要處理的事件為EPOLLOUT

                //epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
            }
            else if (events[i].events & EPOLLOUT) // 如果有資料傳送

            {
                sockfd = events[i].data.fd;
                write(sockfd, line, n);
                //設定用於讀操作的檔案描述符

                ev.data.fd = sockfd;
                //設定用於注測的讀操作事件

                ev.events = EPOLLIN | EPOLLET;
                //修改sockfd上要處理的事件為EPOLIN

                epoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &ev);
            }
        }
    }
    return 0;
}

TCP客戶端

#include <arpa/inet.h>
#include <errno.h>
#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#define BUF_SIZE 128
void client_job(int sd);
int main(int argc, char **argv)
{
    int sockfd;
    struct sockaddr_in my_addr; //賦值時用這個結構
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    my_addr.sin_family = AF_INET;
    my_addr.sin_port = htons(5555);
    my_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
    bzero(&(my_addr.sin_zero), 8);
    if (connect(sockfd, (sockaddr *)&my_addr, sizeof(my_addr)) < 0)
    {
        perror("connect()2");
        exit(1);
    }
    client_job(sockfd);
    close(sockfd);
    exit(0);
}
void client_job(int sd)
{
    char send_buf[BUF_SIZE];
    sprintf(send_buf, "hello");
    while (1)
    {
        if (send(sd, send_buf, 5, 0) < 0)
        {
            perror("send");
            exit(1);
        }
        usleep(1000 * 500);
    }
}

 

相關文章