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

新媒体 网站建设 管理规范百度收录提交申请

新媒体 网站建设 管理规范,百度收录提交申请,wordpress显示文章内容,wordpress 中文杂志主题read函数的行为 read返回0表示正常结束条件,而非错误,具体场景包括: 文件读取结束,到达文件末尾(EOF)连接关闭:在socket通信中,对端正常关闭连接(如执行close()或进程终…

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)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.

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

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.dtcms.com/wzjs/423102.html

相关文章:

  • 资阳网站建设实体店怎么引流推广
  • 灌云网站建设维护网络视频营销的案例
  • 表白网站制作器百度关键词首页排名
  • 成都公安网站备案兰州seo公司
  • 住房和城乡建设部网站住房补贴企业推广app
  • 企业建设营销型网站步骤网络营销的主要方法
  • 徐州手机网站建设公司百度推广关键词和创意
  • 网站搭建中114514西安 做网站
  • 公司网站建设案例市场运营和市场营销的区别
  • 网站建设的生存期模型网站多少钱
  • 网站开发四点注意事项正规推广赚佣金的平台
  • 工程网站建设系统优化助手
  • 输入网址跳到别的网站怎么提高关键词搜索排名
  • 美国旅游网站排名网络seo关键词优化技术
  • 湖南网站备案2023能用的磁力搜索引擎
  • 手机网站 win8风格友情链接管理系统
  • 做兼职的设计网站佛山关键词排名效果
  • 注册域名之后如何建设网站长沙seo排名扣费
  • 网页微博如何注销站内优化
  • gzip压缩网站武汉seo关键字优化
  • 嘉定网站建设哪家好淘宝数据分析
  • 大连网站建设-网龙科技草根站长工具
  • 通化网站建设单位网上销售培训课程
  • 商丘做网站汉狮网络郑州网站优化外包顾问
  • 天河网站建设制作广东seo网站推广
  • 网站做等保二级收费多少seo推广员是做什么的
  • ftp网站怎么建平台推广营销
  • wordpress 文章作者搜外seo
  • wordpress付费下载功能沈阳seo排名外包
  • 郑州建设信息网网四川旅游seo整站优化