写入失败,errno 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用调试器单步调试代码,并确保每个语句都完全按照您认为应该执行的操作。我打赌你会发现一个更早的错误。
此外,
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 andBUF_SIZE
.你检查过 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.
我总是在读或写或此类调用之后执行类似的操作。
I'd do something like this always following a read or write or such calls.