Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO多路复用:poll #31

Open
manxisuo opened this issue Aug 27, 2022 · 0 comments
Open

IO多路复用:poll #31

manxisuo opened this issue Aug 27, 2022 · 0 comments

Comments

@manxisuo
Copy link
Owner

manxisuo commented Aug 27, 2022

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/poll.h>

#define BUF_SIZE 1024
#define OPEN_MAX 1024
#define SERVER_PORT 10001
#define INFTIM -1

int main(int argc, char *argv[])
{
    char buf[BUF_SIZE];  // 缓冲区

    // 新建Socket
    // AF_INET:IP协议族
    int listenFd = socket(AF_INET, SOCK_STREAM, 0);

    // 设置服务器地址
    struct sockaddr_in serverAddr;
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(SERVER_PORT);
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);

    // 将Socket绑定到上述地址
    bind(listenFd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));   
    listen(listenFd, 100);  // 开始接受连接

    struct pollfd fds[OPEN_MAX];
    fds[0].fd = listenFd;
    fds[0].events = POLLRDNORM;

    for (int i = 1; i < OPEN_MAX; i++) fds[i].fd = -1;

    int maxIndex = 0;

    for (;;)
    {
        int nready = poll(fds, maxIndex + 1, INFTIM);

        if (fds[0].revents & POLLRDNORM)
        {
            // 获取客户端连接生成的Socket
            struct sockaddr_in clientAddr;
            socklen_t clientLen = sizeof (clientAddr);
            int connFd = accept(listenFd, (struct sockaddr*)&clientAddr, &clientLen);

            printf("Socket %d created\n", connFd);
            fflush(stdout);

            int i;
            for (i = 1; i < OPEN_MAX; i++)
            {
                if (fds[i].fd < 0)
                {
                    fds[i].fd = connFd;
                    fds[i].events = POLLRDNORM;
                    break;
                }
            }

            if (i == OPEN_MAX)
            {
                printf("too many clients!\n");
            }

            if (i > maxIndex) maxIndex = i;

            nready -= 1;
            if (nready <= 0) continue;
        }

        for (int i = 1; i <= maxIndex; i++)
        {
            if (fds[i].fd < 0) continue;
            int sockFd = fds[i].fd;
            if (fds[i].revents & (POLLRDNORM | POLLERR))
            {
                int n = read(sockFd, buf, BUF_SIZE);
                if (n == 0)
                {
                    close(sockFd);
                    fds[i].fd = -1;
                    printf("Socket %d closed\n", sockFd);
                    fflush(stdout);
                }
                else
                {
                    buf[n] = '\0';
                    write(sockFd, buf, n);
                    printf("Socket %d said : %s\n", sockFd, buf);
                    fflush(stdout);
                }
                nready -= 1;
                if (nready <= 0) break;
            }
        }
    }

    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant