C epoll_wait()成功,但返回-1字节

C epoll_wait()成功,但返回-1字节,c,linux,sockets,posix,C,Linux,Sockets,Posix,下面是我为UNIX域数据报套接字上的epoll_wait创建的代码(注意,这是UNIX域,不是internet域)。这些C程序中的每一个都由一个NASM程序执行——C对象文件链接到NASM可执行文件中 我对epoll\u instance\u create或add\u到\u epoll\u fd\u列表(添加每个文件描述符)没有任何问题。但是,epoll_wait_下一步调用peror并返回“epoll_wait:Success”,然后循环执行“for(i=0;i0) 返回1; 返回0; } i

下面是我为UNIX域数据报套接字上的epoll_wait创建的代码(注意,这是UNIX域,不是internet域)。这些C程序中的每一个都由一个NASM程序执行——C对象文件链接到NASM可执行文件中

我对epoll\u instance\u create或add\u到\u epoll\u fd\u列表(添加每个文件描述符)没有任何问题。但是,epoll_wait_下一步调用peror并返回“epoll_wait:Success”,然后循环执行“for(i=0;i 结构epoll_事件epoll_事件被定义为全局事件,因此在调用这些程序时可以由NASM传递

问题可能是我不明白如何在我的上下文中调用read(),因为代码来自其他代码示例(这是我第一次使用epoll)

#定义基本单位尺寸750
#定义SV_SOCK_路径“/tmp/ud_ucase”
#定义epoll_maxevents 100
结构epoll\u事件epoll\u事件;
int64\u t epoll\u instance\u create(){
int epoll_fd=epoll_create1(0);
返回(int64)epoll\U fd;
}
int64\u t将\u添加到\u epoll\u fd\u列表(int epoll\u fd,int this\u fd){
epoll_events.data.fd=epoll_fd;
epoll_events.events=EPOLLIN;
int res=epoll\u ctl(epoll\u fd、epoll\u ctl\u ADD、this\u fd和epoll\u事件);
perror(“epoll_ctl”);
如果(分辨率>0)
返回1;
返回0;
}
int64 epoll\U wait\U next(int epoll\U fd,结构epoll\U事件*事件){
int事件_计数,i;
ssize_t计数;
ssize_t字节_u u读取;
int64_t读取缓冲区[750];
int64测试数据;
事件计数=epoll\u等待(epoll\u fd,events,epoll\u maxevents,-1);
佩罗尔(epoll_wait);
对于(i=0;i0)
测试数据=读取缓冲区[0];
printf(“读取%zd字节。\n”,读取字节);
读取缓冲区[字节读取]='\0';
}
返回0;
}
整个代码(包括NASM代码)非常大,即使是最小的版本也会超过300行,因此我在上面发布了相关的C程序,我认为这应该足以发现问题


提前感谢您提供帮助,帮助您理解为什么字节读取为-1;它应该是720字节。

请在
读取后立即检查
errno
和/或
perror
,因为它返回
-1
,这表示错误。Re“我没有保护它,因为我想看到“成功”只是为了确认”,
errno
(由
perror
使用)只保证在出现错误时有意义。特别是,它不保证在成功时“成功”。至少在linux上,要“立即发现无效参数”,读取
的手册页非常精确地列出了返回EINVAL(和其他错误)的条件。Re“应该是720字节”,请注意,
read
返回的值总是小于请求的值。(但这里并不是这样。)Re“我们假设如果没有报告错误,那么就没有错误?”,是的,如果
read
返回值
=0
,则不会发生错误。///Re“如果返回”成功“没有错误,这不意味着没有错误吗?”,是的,但相反的说法是不正确的。它输出的不是成功,也不意味着有错误。请在
读取
后立即检查
错误号
和/或
perror
,因为它返回
-1
,表示有错误。Re”我没有保护它,因为我想看到“成功”只是为了确认“,
errno
(由
perror
使用)只保证在出错时有意义。特别是,它不保证在成功时“成功”。现在重新“确定”以发现无效的参数“,至少在linux上,
read
的手册页非常精确地列出了返回EINVAL(和其他错误)的条件。Re“应该是720字节”,请注意,
read
的返回总是比请求的少。(不过,这里并不是这样。)“我们假设如果没有报告错误,那么就没有错误?”,是的,如果
read
返回
=0
,就没有错误发生。///Re“如果它返回“Success”而没有错误,这不意味着没有错误吗?“,是的,但反过来不是真的。它输出的不是成功,并不意味着有错误。
#define BUF_SIZE 750
#define SV_SOCK_PATH "/tmp/ud_ucase"
#define epoll_maxevents 100

struct epoll_event epoll_events;

int64_t epoll_instance_create() {
    int epoll_fd = epoll_create1(0);
    return (int64_t) epoll_fd;
}

int64_t add_to_epoll_fd_list(int epoll_fd, int this_fd) {
    epoll_events.data.fd = epoll_fd;
    epoll_events.events = EPOLLIN;
    int res = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, this_fd,   &epoll_events);
    perror("epoll_ctl");

    if (res > 0)
        return 1;

    return 0;
}

int64_t epoll_wait_next(int epoll_fd, struct epoll_event * events){

    int event_count, i;
    ssize_t count;
    ssize_t bytes_read;
    int64_t read_buffer[750];
    int64_t test_data;

    event_count = epoll_wait(epoll_fd, events, epoll_maxevents, -   1);
    perror("epoll_wait");

    for(i = 0; i < event_count; i++) {

    printf("Reading file descriptor '%d' -- ", events[i].data.fd);

    bytes_read = read(events[i].data.fd, read_buffer, BUF_SIZE);

    if (bytes_read > 0)
        test_data = read_buffer[0];

    printf("%zd bytes read.\n", bytes_read);
    read_buffer[bytes_read] = '\0';
    }

    return 0;
}