linux,socket编程,select,poll,epoll学习

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> // 添加这一行
#include <string.h>    // 添加这一行
#include <pthread.h>
#include <poll.h>
#include <sys/epoll.h>

void *handle_client(void *arg)
{
    int clientfd = *(int *)arg;
    char buf[1024];
    printf("clientfd:%d\n", clientfd);
    while (1)
    {
        memset(buf, 0, sizeof(buf));
        int ret = recv(clientfd, buf, sizeof(buf), 0);
        if (ret <= 0)
        {
            printf("客户端断开连接\n");
            break;
        }
        printf("recv:%s\n", buf);
        char ans[] = "yesmefasrefas";
        int len = sizeof(ans);
        send(clientfd, ans, len, 0);
    }
    close(clientfd);
}
int main()
{
    // 创建socket_fd;
    printf("创建socket\n");
    int socketfd = socket(AF_INET, SOCK_STREAM, 0);
    if (socketfd < 0)
    {
        perror("socket");
        return -1;
    }

    // 绑定ip地址
    printf("绑定ip\n");
    struct sockaddr_in serveraddr;
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(9999);
    serveraddr.sin_addr.s_addr = inet_addr("0.0.0.0");
    if (bind(socketfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
    {
        perror("bind");
        return -1;
    }
    listen(socketfd, 10);
    printf("等待客户端连接\n");

    struct sockaddr_in clientaddr;
    int len = sizeof(clientaddr);
    printf("sockfd:%d\n", socketfd);

#if 0

    int clientfd = accept(socketfd, (struct sockaddr *)&clientaddr, &len);
    printf("客户端连接成功\n");
    char client_ip[INET_ADDRSTRLEN];
    inet_ntop(AF_INET, &(clientaddr.sin_addr), client_ip, INET_ADDRSTRLEN);
    printf("Client connected from IP: %s\n", client_ip);

#endif

#if 0
    //多线程
    while (1)
    {
        int clientfd = accept(socketfd, (struct sockaddr *)&clientaddr, &len);
        printf("客户端连接成功\n");
        pthread_t pid_t;
        pthread_create(&pid_t, NULL, handle_client, &clientfd);
    }
#endif
#if 0
    // select();
    fd_set rfds, rset;
    FD_ZERO(&rfds);          // 全部置0
    FD_SET(socketfd, &rfds); // sockfd 置1
    int maxfd = socketfd;
    while (1)
    {
        rset = rfds;
        int ready = select(maxfd + 1, &rset, NULL, NULL, NULL);
        if (FD_ISSET(socketfd, &rset)) // 判断是否在里面
        {
            int clientfd = accept(socketfd, (struct sockaddr *)&clientaddr, &len);

            printf("有情况!clientfd: %d\n", clientfd);
            FD_SET(clientfd, &rfds);
            maxfd = clientfd;
        }

        int i = 0;

        for (i = socketfd + 1; i <= maxfd; i++)
        {
            if (FD_ISSET(i, &rset))
            {
                printf("有数据到达了,fd:%d\n", i);
                char buf[1024];
                // printf("clientfd:%d\n", clientfd);
                memset(buf, 0, sizeof(buf));
                int ret = recv(i, buf, sizeof(buf), 0);
                if (ret <= 0)
                {
                    printf("客户端断开连接\n");
                    break;
                }
                printf("recv:%s\n", buf);
                char ans[] = "yesmefasrefas";
                int len = sizeof(ans);
                send(i, ans, len, 0);
                FD_CLR(i, &rfds);
                close(i);
            }
        }
    }

#endif

#if 0
    // poll();
    struct pollfd fds[1024];
    fds[socketfd].fd = socketfd;
    fds[socketfd].events = POLLIN;
    int maxfd = socketfd;
    while (1)
    {
        poll(fds, 1024, socketfd);
        if (fds[socketfd].revents & POLLIN)
        {
            int clientfd = accept(socketfd, (struct sockaddr *)&clientaddr, &len);
            printf("有情况!clientfd: %d\n", clientfd);
            fds[clientfd].fd = clientfd;
            fds[clientfd].events = POLLIN;
            maxfd = clientfd;
        }

        int i = 0;
        for (i = socketfd + 1; i <= maxfd; i++)
        {
            if (fds[i].revents & POLLIN)
            {
                printf("有数据到达了,fd:%d\n", i);
                char buf[1024];
                // printf("clientfd:%d\n", clientfd);
                memset(buf, 0, sizeof(buf));
                int ret = recv(i, buf, sizeof(buf), 0);
                if (ret <= 0)
                {
                    printf("客户端断开连接\n");
                    fds[i].fd = -1;
                    fds[i].events = 0;
                    close(i);
                }
                else
                {
                    printf("recv:%s\n", buf);
                    char ans[] = "yesmefasrefas";
                    int len = sizeof(ans);
                    send(i, ans, len, 0);
                }
            }
        }
    }

#endif

#if 1
    // epoll
    int epfd = epoll_create(1024);
    struct epoll_event ev;
    ev.events = EPOLLIN;
    ev.data.fd = socketfd;

    epoll_ctl(epfd, EPOLL_CTL_ADD, socketfd, &ev);
    struct epoll_event events[1024];
    while (1)
    {
        int ready = epoll_wait(epfd, events, 1024, -1);
        int i = 0;
        for (i = 0; i < ready; i++)
        {
            int connfd = events[i].data.fd;
            if (connfd == socketfd)
            {
                int clientfd = accept(socketfd, (struct sockaddr *)&clientaddr, &len);
                printf("有情况!clientfd: %d\n", clientfd);

                ev.data.fd = clientfd;
                ev.events = EPOLLIN;
                epoll_ctl(epfd, EPOLL_CTL_ADD, clientfd, &ev);
            }
            else if (events[i].events & EPOLLIN)
            {
                printf("有数据到达了,fd:%d\n", connfd);
                char buf[1024];
                // printf("clientfd:%d\n", clientfd);
                memset(buf, 0, sizeof(buf));
                int l = sizeof(buf);

                int ret = recv(connfd, buf, 5, 0);
                if (ret <= 0)
                {
                    printf("客户端断开连接\n");
                    epoll_ctl(epfd, EPOLL_CTL_DEL, connfd, NULL);
                    close(connfd);
                }
                else
                {
                    printf("recv:%s\n", buf);
                    char ans[] = "yesmefasrefas";
                    int len = sizeof(ans);
                    send(connfd, ans, len, 0);
                }
            }
        }
    }
#endif

    close(socketfd);
    return 0;
}

client代码

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> // 添加这一行
#include <string.h>    // 添加这一行
#include <pthread.h>

int main(int argc, char *argv[])
{
    int sockfd;
    struct sockaddr_in servaddr;
    char buf[1024];
    int n;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(9999);
    servaddr.sin_addr.s_addr = inet_addr("113.45.173.146");
    connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    char buffrecv[] = "123456789123456789123456789";
    send(sockfd, buffrecv, sizeof(buffrecv), 0);
    recv(sockfd, buf, 1024, 0);
    printf("recv:%s\n", buf);
    sleep(3);
    close(sockfd);
    return 0;
}
相关推荐
The_Ticker5 分钟前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
大数据编程之光28 分钟前
Flink Standalone集群模式安装部署全攻略
java·大数据·开发语言·面试·flink
我言秋日胜春朝★37 分钟前
【Linux】进程地址空间
linux·运维·服务器
爪哇学长41 分钟前
双指针算法详解:原理、应用场景及代码示例
java·数据结构·算法
风尚云网1 小时前
风尚云网前端学习:一个简易前端新手友好的HTML5页面布局与样式设计
前端·css·学习·html·html5·风尚云网
ExiFengs1 小时前
实际项目Java1.8流处理, Optional常见用法
java·开发语言·spring
paj1234567891 小时前
JDK1.8新增特性
java·开发语言
繁依Fanyi1 小时前
简易安卓句分器实现
java·服务器·开发语言·算法·eclipse
C-cat.1 小时前
Linux|环境变量
linux·运维·服务器
慧都小妮子1 小时前
Spire.PDF for .NET【页面设置】演示:打开 PDF 时自动显示书签或缩略图
java·pdf·.net