read_0">read函数的行为
read
返回0表示正常结束条件,而非错误,具体场景包括:
- 文件读取结束,到达文件末尾(EOF)
- 连接关闭:在socket通信中,对端正常关闭连接(如执行close()或进程终止)
- 管道/流结束:从管道或FIFO读取时写入端已关闭
此时不需要处理错误码,应作为正常逻辑分支处理。
当read
返回-1时,需通过errno
判断具体错误类型:
错误码 | 触发场景 | 处理建议 |
---|---|---|
EAGAIN | 非阻塞模式下无数据可读(与EWOULDBLOCK等价) | 等待后重试或切换为阻塞模式 |
EBADF | 无效文件描述符(如未打开/已关闭的fd) | 检查fd生命周期管理逻辑 |
EFAULT | 缓冲区指针越界(buf指向非法内存地址) | 检查内存分配和指针有效性 |
EINTR | 系统调用被信号中断 | 重启read调用(需实现重试逻辑) |
EINVAL | 文件描述符不支持读取(如O_DIRECT模式参数错误) | 检查open时的打开模式 |
EIO | 底层I/O错误(如磁盘故障、网络硬件错误) | 检查硬件状态,可能需要终止操作 |
EISDIR | 尝试读取目录文件(需通过readdir访问目录)[^系统标准] | 修改代码逻辑,区分文件和目录访问方式 |
read_24">man 2 read
READ(2) Linux Programmer's Manual READ(2)NAMEread - read from a file descriptorSYNOPSIS#include <unistd.h>ssize_t read(int fd, void *buf, size_t count);DESCRIPTIONread() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.On files that support seeking, the read operation commences at the current file offset, and the file offset is incremented by the number of bytes read. If the current fileoffset is at or past the end of file, no bytes are read, and read() returns zero.If count is zero, read() may detect the errors described below. In the absence of any errors, or if read() does not check for errors, a read() with a count of 0 returnszero and has no other effects.If count is greater than SSIZE_MAX, the result is unspecified.RETURN VALUEOn success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number issmaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, orbecause we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. On error, -1 is returned, and errno is set appropriately. In thiscase it is left unspecified whether the file position (if any) changes.ERRORSEAGAIN The file descriptor fd refers to a file other than a socket and has been marked nonblocking (O_NONBLOCK), and the read would block.EAGAIN or EWOULDBLOCKThe file descriptor fd refers to a socket and has been marked nonblocking (O_NONBLOCK), and the read would block. POSIX.1-2001 allows either error to be returnedfor this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.EBADF fd is not a valid file descriptor or is not open for reading.EFAULT buf is outside your accessible address space.EINTR The call was interrupted by a signal before any data was read; see signal(7).EINVAL fd is attached to an object which is unsuitable for reading; or the file was opened with the O_DIRECT flag, and either the address specified in buf, the value speci‐fied in count, or the current file offset is not suitably aligned.EINVAL fd was created via a call to timerfd_create(2) and the wrong size buffer was given to read(); see timerfd_create(2) for further information.EIO I/O error. This will happen for example when the process is in a background process group, tries to read from its controlling terminal, and either it is ignoring orblocking SIGTTIN or its process group is orphaned. It may also occur when there is a low-level I/O error while reading from a disk or tape.EISDIR fd refers to a directory.Other errors may occur, depending on the object connected to fd. POSIX allows a read() that is interrupted after reading some data to return -1 (with errno set to EINTR)or to return the number of bytes already read.
readint_82">read错误码对应int
#include <errno.h>
#include <stdio.h>int main()
{printf("EAGAIN = %d\n", EAGAIN);printf("EBADF = %d\n", EBADF);printf("EFAULT = %d\n", EFAULT);printf("EINTR = %d\n", EINTR);printf("EINVAL = %d\n", EINVAL);printf("EIO = %d\n", EIO);printf("EISDIR = %d\n", EISDIR);return 0;
}
EAGAIN = 11
EBADF = 9
EFAULT = 14
EINTR = 4
EINVAL = 22
EIO = 5
EISDIR = 21
read_110">read实例
第一步:服务端执行net
$ ./net
Waiting for client connection...
第二步:服务端监听8484端口
···
sudo tcpdump -i lo -X ‘port 8484’
···
第三步:本机telnet 127.0.0.1 8484
$ telnet 127.0.0.1 8484
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
tcpdump记录三次握手
11:18:15.624498 IP VM-128-138-tencentos.33708 > VM-128-138-tencentos.8484: Flags [S], seq 61924722, win 65495, options [mss 65495,sackOK,TS val 357905057 ecr 0,nop,wscale 7], length 00x0000: 4510 003c 2a24 4000 4006 1286 7f00 0001 E..<*$@.@.......0x0010: 7f00 0001 83ac 2124 03b0 e572 0000 0000 ......!$...r....0x0020: a002 ffd7 fe30 0000 0204 ffd7 0402 080a .....0..........0x0030: 1555 32a1 0000 0000 0103 0307 .U2.........
11:18:15.624525 IP VM-128-138-tencentos.8484 > VM-128-138-tencentos.33708: Flags [S.], seq 3935336572, ack 61924723, win 65483, options [mss 65495,sackOK,TS val 357905057 ecr 357905057,nop,wscale 7], length 00x0000: 4500 003c 0000 4000 4006 3cba 7f00 0001 E..<..@.@.<.....0x0010: 7f00 0001 2124 83ac ea90 787c 03b0 e573 ....!$....x|...s0x0020: a012 ffcb fe30 0000 0204 ffd7 0402 080a .....0..........0x0030: 1555 32a1 1555 32a1 0103 0307 .U2..U2.....
11:18:15.624539 IP VM-128-138-tencentos.33708 > VM-128-138-tencentos.8484: Flags [.], ack 1, win 512, options [nop,nop,TS val 357905057 ecr 357905057], length 00x0000: 4510 0034 2a25 4000 4006 128d 7f00 0001 E..4*%@.@.......0x0010: 7f00 0001 83ac 2124 03b0 e573 ea90 787d ......!$...s..x}0x0020: 8010 0200 fe28 0000 0101 080a 1555 32a1 .....(.......U2.0x0030: 1555 32a1
第四步:telnet进程被Kill -9,tcpdump抓到结束包发回给client,可以看到Flags [F.] fin互发了一次。服务端的read函数收到fin后,返回0,表示连接关闭了。
11:20:04.707587 IP VM-128-138-tencentos.33708 > VM-128-138-tencentos.8484: Flags [F.], seq 1, ack 1, win 512, options [nop,nop,TS val 358014140 ecr 357905057], length 00x0000: 4510 0034 2a26 4000 4006 128c 7f00 0001 E..4*&@.@.......0x0010: 7f00 0001 83ac 2124 03b0 e573 ea90 787d ......!$...s..x}0x0020: 8011 0200 fe28 0000 0101 080a 1556 dcbc .....(.......V..0x0030: 1555 32a1 .U2.
11:20:04.707674 IP VM-128-138-tencentos.8484 > VM-128-138-tencentos.33708: Flags [F.], seq 1, ack 2, win 512, options [nop,nop,TS val 358014140 ecr 358014140], length 00x0000: 4500 0034 203b 4000 4006 1c87 7f00 0001 E..4.;@.@.......0x0010: 7f00 0001 2124 83ac ea90 787d 03b0 e574 ....!$....x}...t0x0020: 8011 0200 fe28 0000 0101 080a 1556 dcbc .....(.......V..0x0030: 1556 dcbc .V..
11:20:04.707681 IP VM-128-138-tencentos.33708 > VM-128-138-tencentos.8484: Flags [.], ack 2, win 512, options [nop,nop,TS val 358014140 ecr 358014140], length 00x0000: 4510 0034 2a27 4000 4006 128b 7f00 0001 E..4*'@.@.......0x0010: 7f00 0001 83ac 2124 03b0 e574 ea90 787e ......!$...t..x~0x0020: 8010 0200 fe28 0000 0101 080a 1556 dcbc .....(.......V..0x0030: 1556 dcbc .V..
谁先发的fin谁进入time_wait,等两个MSL(报文最大存活时间)后,才会解除端口占用。
$ netstat -nap | grep 33708
(Not all processes could be identified, non-owned process infowill not be shown, you would have to be root to see it all.)
tcp 0 0 127.0.0.1:8484 127.0.0.1:33708 ESTABLISHED 11208/./net
tcp 0 0 127.0.0.1:33708 127.0.0.1:8484 ESTABLISHED 11264/telnet$ kill -9 11264$ netstat -nap | grep 33708
(Not all processes could be identified, non-owned process infowill not be shown, you would have to be root to see it all.)
tcp 0 0 127.0.0.1:33708 127.0.0.1:8484 TIME_WAIT -
read_185">read实例代码
net.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>#define BUFFER_SIZE 256int main()
{int server_fd, client_fd;struct sockaddr_in server_addr, client_addr;socklen_t client_len = sizeof(client_addr);char buffer[BUFFER_SIZE];if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){perror("socket creation failed");exit(EXIT_FAILURE);}server_addr.sin_family = AF_INET;server_addr.sin_addr.s_addr = INADDR_ANY;server_addr.sin_port = htons(8484);if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0){perror("bind failed");close(server_fd);exit(EXIT_FAILURE);}if (listen(server_fd, 5) < 0){perror("listen failed");close(server_fd);exit(EXIT_FAILURE);}printf("Waiting for client connection...\n");if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len)) < 0){perror("accept failed");close(server_fd);exit(EXIT_FAILURE);}printf("Client connected from %s:%d\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));while (1){ssize_t bytes_read = read(client_fd, buffer, BUFFER_SIZE - 1);if (bytes_read > 0){buffer[bytes_read] = '\0';printf("Received: %s\n", buffer);}else if (bytes_read == 0){printf("Connection closed by client[5](@ref)\n");break;}else{perror("read error");break;}}close(client_fd);close(server_fd);return 0;
}