下面是修改后的代码，使用connect函数将UDP套接字连接到固定的IP地址和端口：

#include <sys/epoll.h>
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>

int main() {
    // 创建UDP套接字
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == -1) {
        std::cerr << “Failed to create socket” << std::endl;
        return 1;
    }

    // 将UDP套接字连接到固定的IP地址和端口
    struct sockaddr_in serverAddr{};
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(2000);
    serverAddr.sin_addr.s_addr = inet_addr(“192.178.111.1”);
    if (connect(sockfd, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) == -1) {
        std::cerr << “Failed to connect socket” << std::endl;
        close(sockfd);
        return 1;
    }

    // 创建epoll实例
    int epollfd = epoll_create(1);
    if (epollfd == -1) {
        std::cerr << “Failed to create epoll instance” << std::endl;
        close(sockfd);
        return 1;
    }

    // 添加套接字到epoll
    struct epoll_event ev{};
    ev.events = EPOLLIN | EPOLLOUT;  // 监听可读和可写事件
    ev.data.fd = sockfd;
    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev) == -1) {
        std::cerr << “Failed to add socket to epoll” << std::endl;
        close(epollfd);
        close(sockfd);
        return 1;
    }

    while (true) {
        // 等待事件发生
        struct epoll_event events[1];
        int numEvents = epoll_wait(epollfd, events, 1, -1);
        if (numEvents == -1) {
            std::cerr << “Error in epoll_wait” << std::endl;
            break;
        }

        // 检查每个返回的事件
        for (int i = 0; i < numEvents; ++i) {
            int fd = events[i].data.fd;

            if (events[i].events & EPOLLIN) {
                // 套接字可读，接收数据
                char buffer[1024];
                memset(buffer, 0, sizeof(buffer));
                ssize_t bytesRead = recv(fd, buffer, sizeof(buffer) - 1, 0);
                if (bytesRead == -1) {
                    std::cerr << “Failed to receive data” << std::endl;
                    break;
                }
                std::cout << "Received data: " << buffer << std::endl;
            }

            if (events[i].events & EPOLLOUT) {
                // 套接字可写，发送数据
                std::string sendData = “Hello, server!”;
                ssize_t bytesSent = send(fd, sendData.c_str(), sendData.size(), 0);
                if (bytesSent == -1) {
                    std::cerr << “Failed to send data” << std::endl;
                    break;
                }
                std::cout << "Sent data: " << sendData << std::endl;

                // 取消对可写事件的监视
                struct epoll_event modEv{};
                modEv.events = EPOLLIN;
                modEv.data.fd = fd;
                if (epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &modEv) == -1) {
                    std::cerr << “Failed to modify epoll event” << std::endl;
                    break;
                }
            }
        }
    }

    // 关闭套接字和epoll实例
    close(epollfd);
    close(sockfd);

    return 0;
}


在代码中，添加了以下几行来连接到固定的IP地址和端口：

// 将UDP套接字连接到固定的IP地址和端口
struct sockaddr_in serverAddr{};
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(2000);
serverAddr.sin_addr.s_addr = inet_addr(“192.178.111.1”);
if (connect(sockfd, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) == -1) {
    std::cerr << “Failed to connect socket” << std::endl;
    close(sockfd);
    return 1;
}


这样，UDP套接字便会发送和接收数据到指定的IP地址（192.178.111.1）和端口（2000）了。请注意，UDP是无连接的协议，所以对于UDP套接字，connect函数实际上只会设置一个默认的目标地址，但并不实际建立连接。
以上你是上次给我的代码，我想给成server地址为INADDR_ANY，client的IP地址（192.178.111.1）和端口（2000）