当前位置: 首页 > news >正文

客户端进程突然结束,服务端read是什么行为?

read函数的行为

read返回0表示正常结束条件,而非错误,具体场景包括:

  1. 文件读取结束,到达文件末尾(EOF)
  2. 连接关闭:在socket通信中,对端正常关闭连接(如执行close()或进程终止)
  3. 管道/流结束:从管道或FIFO读取时写入端已关闭

此时不需要处理错误码,应作为正常逻辑分支处理。

read返回-1时,需通过errno判断具体错误类型

错误码触发场景处理建议
EAGAIN非阻塞模式下无数据可读(与EWOULDBLOCK等价)等待后重试或切换为阻塞模式
EBADF无效文件描述符(如未打开/已关闭的fd)检查fd生命周期管理逻辑
EFAULT缓冲区指针越界(buf指向非法内存地址)检查内存分配和指针有效性
EINTR系统调用被信号中断重启read调用(需实现重试逻辑)
EINVAL文件描述符不支持读取(如O_DIRECT模式参数错误)检查open时的打开模式
EIO底层I/O错误(如磁盘故障、网络硬件错误)检查硬件状态,可能需要终止操作
EISDIR尝试读取目录文件(需通过readdir访问目录)[^系统标准]修改代码逻辑,区分文件和目录访问方式

man 2 read

READ(2)                                                                      Linux Programmer's Manual                                                                      READ(2)

NAME
       read - read from a file descriptor

SYNOPSIS
       #include <unistd.h>

       ssize_t read(int fd, void *buf, size_t count);

DESCRIPTION
       read() 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 file
       offset 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  returns
       zero and has no other effects.

       If count is greater than SSIZE_MAX, the result is unspecified.

RETURN VALUE
       On  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 is
       smaller 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, or
       because  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 this
       case it is left unspecified whether the file position (if any) changes.

ERRORS
       EAGAIN 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 EWOULDBLOCK
              The 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  returned
              for 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 or
              blocking 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.

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实例

第一步:服务端执行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 0
	0x0000:  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 0
	0x0000:  4500 003c 0000 4000 4006 3cba 7f00 0001  E..<..@.@.<.....
	0x0010:  7f00 0001 2124 83ac ea90 787c 03b0 e573  ....!$....x|...s
	0x0020:  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 0
	0x0000:  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 0
	0x0000:  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 0
	0x0000:  4500 0034 203b 4000 4006 1c87 7f00 0001  E..4.;@.@.......
	0x0010:  7f00 0001 2124 83ac ea90 787d 03b0 e574  ....!$....x}...t
	0x0020:  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 0
	0x0000:  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 info
 will 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 info
 will 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实例代码

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 256

int 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;
}

相关文章:

  • 计算机三级网络技术备考
  • Android 字体大小自动处理 AppCompactTextView 和 自定义 TextView
  • 密码学基础
  • 【c语言】字符函数和字符串函数(1)
  • ue学习part2
  • 安全开发-环境选择
  • 日语学习-日语知识点小记-构建基础-JLPT-N4N5阶段(11): 助动词使用 なります&なりました:復習(ふくしゅう)
  • 游戏引擎学习第123天
  • 洛谷每日1题-------Day1__超级玛丽游戏
  • 【笔记】redis回忆录(未完 重头过一遍)
  • 使用elasticdump导出/导入 -- ES数据
  • 数据安全_笔记系列06:数据生命周期管理(存储、传输、使用、销毁)深度解析
  • 开发一个交易所需要哪些技术?
  • C++:继承
  • WordPress R+L Carrier Edition sql注入漏洞复现(CVE-2024-13481)(附脚本)
  • 高效管理 React 状态和交互:我的自定义 Hooks 实践
  • BigDecimal线上异常解决方案:避免科学计数法输出的坑
  • Maven 依赖的深入理解(一)
  • 告别 Freetype,拥抱高效字体处理新方案 - 纯c#解析字体库
  • Ajax数据采集与分析详解
  • 我使馆就中国公民和企业遭不公正待遇向菲方持续提出严正交涉
  • 证监会发布《上市公司募集资金监管规则》,6月15日起施行
  • 病重老人被要求亲自取钱在农业银行门口去世?株洲警方介入
  • 万科再获深铁集团借款,今年已累计获股东借款近120亿元
  • “老中青少”四代同堂,季春艳携锡剧《玲珑女》冲击梅花奖
  • 外企聊营商|特雷通集团:税务服务“及时雨”