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

devtools/2025/2/27 2:59:21/

read_0">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访问目录)[^系统标准]修改代码逻辑,区分文件和目录访问方式

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

http://www.ppmy.cn/devtools/162942.html

相关文章

新书速览|鸿蒙HarmonyOS NEXT开发之路 卷1:ArkTS语言篇

《鸿蒙HarmonyOS NEXT开发之路 卷1&#xff1a;ArkTS语言篇》 1 本书内容 《鸿蒙HarmonyOS NEXT开发之路 卷1&#xff1a;ArkTS语言篇》全面、深入地介绍华为HarmonyOS NEXT操作系统中的ArkTS语言。《鸿蒙HarmonyOS NEXT开发之路 卷1&#xff1a;ArkTS语言篇》分为基础知识、A…

【找工作】C++和算法复习(自用)

文章目录 C头文件自定义排序函数stl 算法数据结构树状数组 数学字符串manacherkmp 自用随便记录 C 排序 stl 头文件 全能头文件&#xff1a; #include<bits/stdc.h>自定义排序函数 bool compare(const int &odd1,const int &odd2) {return odd1>odd2; }…

【Matlab仿真】Matlab Function中如何使用静态变量?

背景 根据Simulink的运行机制&#xff0c;每个采样点会调用一次MATLAB Function的函数&#xff0c;两次调用之间&#xff0c;同一个变量的前次计算的终值如何传递到当前计算周期来&#xff1f;其实可以使用persistent变量实现函数退出和进入时内部变量值的保持。 persistent变…

Android构建系统 - 02 初始化编译环境,添加产品

文章目录 初始化编译环境&#xff0c;选择产品envsetup.sh脚本不开启 subshell作用提供实用函数添加编译选项查找/执行 其它vendorsetup.sh lunch ProductProduct 概念编译选项解析层级配置文件目录AOSP 预制芯片及方案厂商 lunch命令作用编译目标BUILD 编译目标BUILDTYPE 编译…

Python黑客技术实战指南:从网络渗透到安全防御

&#x1f31f; 嗨&#xff0c;我是Lethehong&#xff01;&#x1f31f; &#x1f30d; 立志在坚不欲说&#xff0c;成功在久不在速&#x1f30d; &#x1f680; 欢迎关注&#xff1a;&#x1f44d;点赞⬆️留言收藏&#x1f680; &#x1f340;欢迎使用&#xff1a;小智初学…

微信小程序:多菜单栏设计效果

一、实现效果 二、代码 wxml 编辑前端界面,步骤 菜单逻辑: 逐步取出数组中的项,首先取出顶部菜单项,然后选中后取出选中的底部数据(左侧菜单+右侧内容),然后点击左侧菜单取出选中的左侧菜单对应的右侧内容 ①这里我的数据是全部封装到一个数组对象的,首先我的循环…

【链 表】

【链表】 一级目录1. 基本概念2. 算法分析2.1 时间复杂度2.2 空间复杂度2.3 时空复杂度互换 线性表的概念线性表的举例顺序表的基本概念顺序表的基本操作1. 初始化2. 插入操作3. 删除操作4. 查找操作5. 遍历操作 顺序表的优缺点总结优点缺点 树形结构图形结构单链表基本概念链表…

DeepSeek 助力 Vue 开发:打造丝滑的表单验证(Form Validation)

前言&#xff1a;哈喽&#xff0c;大家好&#xff0c;今天给大家分享一篇文章&#xff01;并提供具体代码帮助大家深入理解&#xff0c;彻底掌握&#xff01;创作不易&#xff0c;如果能帮助到大家或者给大家一些灵感和启发&#xff0c;欢迎收藏关注哦 &#x1f495; 目录 Deep…