写入失败,errno 0

发布于 2024-09-06 03:02:56 字数 303 浏览 9 评论 0原文

我有一个客户端服务器情况,我使用接收数据

read(socket, char_buf, BUF_SIZE)

,然后尝试使用将其写入日志文件,

write(filefd, char_buf, strlen(char_buf))

奇怪的是,这失败了(写入返回-1),但 errno 设置为 0,我可以打印消息,并且日志文件描述符有效(我在此命令之前和之后写入它)。

这是怎么回事??

(使用 Linux 内核 2.4(作业))

I have a client server situation in which I receive data using

read(socket, char_buf, BUF_SIZE)

and then try to write it into a log file using

write(filefd, char_buf, strlen(char_buf))

Strangely enough this fails (write returns -1), and yet the errno is set to 0, and I can print the message, AND the log file descriptor works (I write to it before and after this command).

What's going on??

(Working on Linux kernel 2.4 (homework))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡離愁欲言轉身 2024-09-13 03:03:01

使用调试器单步调试代码,并确保每个语句都完全按照您认为应该执行的操作。我打赌你会发现一个更早的错误。

此外,read() 的返回值也很重要。它告诉您实际读取了多少字节。对于成功读取,它可以是 1 到 BUF_SIZE 之间的任意值。

Step through your code with a debugger and make sure that each statement is doing exactly what you think it should be doing. I bet you'll find an earlier bug.

Also, the return value for the read() is important. It tells you how many bytes were actually read. For a successful read, it could be anywhere between 1 and BUF_SIZE.

ぺ禁宫浮华殁 2024-09-13 03:03:00

你检查过 read() 的状态吗?它可能有错误,导致 char_buf 的长度为零。

Did you check the status of your read()? It may have an error, that results in the length of char_buf to be zero.

世态炎凉 2024-09-13 03:02:59
int reads = read(socket, char_buf, BUF_SIZE);

if ( reads != BUF_SIZE )
{
    /* something might have gone wrong */
    fprintf( stderr, "%s\n", strerror( errno ));
}

int writes= = write( filedes, buffer, buffer_size );

if ( writes != buffer_size )
{
    /* something might have gone wrong */
    fprintf( stderr, "%s\n", strerror( errno ));
}

我总是在读或写或此类调用之后执行类似的操作。

int reads = read(socket, char_buf, BUF_SIZE);

if ( reads != BUF_SIZE )
{
    /* something might have gone wrong */
    fprintf( stderr, "%s\n", strerror( errno ));
}

int writes= = write( filedes, buffer, buffer_size );

if ( writes != buffer_size )
{
    /* something might have gone wrong */
    fprintf( stderr, "%s\n", strerror( errno ));
}

I'd do something like this always following a read or write or such calls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文