当前位置:网站首页>SOCKET编程部分I/O的初步解决
SOCKET编程部分I/O的初步解决
2022-06-25 21:32:00 【ypd.】
SOCKET编程部分I/O的初步解决
什么是套接字的部分I/O
- 在许多情况下,当在流式套接字上执行
I/O操作时会出现部分读取和部分写入的现象 - 执行
read()和write()系统调用时,在某些情况下, 可能会出现 被传输的数据 少于 请求的数据
什么时候会出现这种现象
- read():
- 可用数据比read()请求的数据少,此时read()返回可用的字节数
- write():
- write()传输了部分请求的字节后被信号处理进程中断
- 套接字工作在非阻塞模式下(
O_NONBLOCK),可能只传输一部分 - 在部分请求的字节已经完成传输后出现了一个异步错误
如何解决
- 可以重新调用系统调用来完成对全部数据的传输
- 我们对
read()与write()构造其各自对应的包裹函数,其可以循环调用对应的系统调用,确保请求的字节数总是可以得到全部传输
-除非 如果出现错误 或 read()检测到了EOF
SHOW ME THE CODE
- readn()
/* readn() 的参数与read()相同 循环使用了read()系统调用 确保请求的字节数总是能够得到全部传输 ssize_t readn(int fd, void *buffer, size_t count); return number of bytes read, 0 on EOF, -1 on failure */
#include <unistd.h>
#include <errno.h>
//ssize_t read(int fildes, void *buf, size_t nbyte);
ssize_t readn(int fd, void *buffer, size_t n)
{
/* ssize_t : signed long \ signed类型仅仅是为了处理read返回-1的情况,无其他意义 size_t : unsigned long */
ssize_t numRead; // * of Bytes fetched by last read()
size_t totRead; // Total * of bytes read so far
char *buf;
buf = buffer;
for(totRead = 0; totRead < n; ){
numRead = read(fd, buf, n-totRead);
if(numRead == 0){
// EOF
return totRead; // may be 0 if this is first read()
}
if(numRead == -1){
if(errno == EINTR){
continue; // interrupted -> restart read()
}else{
return -1; // some other error
}
}
totRead += numRead;
buf += numRead; // offset
}
return totRead; // must be 'n' Bytes if we get here
}
- writen()
/* writen() 的参数与write()相同 循环使用了write()系统调用 确保请求的字节数总是能够得到全部传输 ssize_t writen(int fd, void *buffer, size_t count); return number of bytes written, -1 on failure */
#include <unistd.h>
#include <errno.h>
//ssize_t write(int fildes, const void *buf, size_t nbyte);
ssize_t writen(int fd, void *buffer, size_t n)
{
/* ssize_t : signed long size_t : unsigned long */
ssize_t numWritten; // * of Bytes fetched by last read()
size_t totWritten; // Total * of bytes read so far
char *buf;
buf = buffer;
for(totWritten = 0; totWritten < n; ){
numWritten = read(fd, buf, n-totWritten);
if(numWritten <= 0){
if(numWritten == -1 && errno == EINTR){
continue; // Interrupted -> restart write()
}else{
return -1;
}
}
totWritten += numWritten;
buf += numWritten;
}
return totWritten; // must be 'n' Bytes if we get here
}
参考
- 《TLPI》
边栏推荐
- CANoe. Diva operation guide - establishment of operation environment
- Input a line of characters to count the English letters, spaces, numbers and other characters
- Is it safe for qiniu school to open a securities account?
- 智云健康上市在即:长期亏损,美年健康俞熔已退出,未来难言乐观
- Robotframework rewrite framework add case control
- After osx-kvm modifies EFI, oc: failed to load configuration, osx-kvm generates opencore Qcow2 reports an error, libguestfs: error: source '
- “No bean named ‘UserController‘ available“
- Canoe learning notes (4)
- Openocd adds third-party device support: ht32f52352 Cortex-M0+
- 启牛学堂证券开户安全嘛?
猜你喜欢

STM32 self balancing robot project, with code, circuit diagram and other data attached at the end (learning materials and learning group at the end)

lombok

QT method of exiting application (exe)

Jmeter- (I) installation of interface test

熊市指南|一些本质的教训与具体的生存法则

Invalid bound statement (not found): com. qf. mapper. PassengerMapper. findByPassengerId

Summary of several methods for FPS calculation
![[buucry] sensor (Manchester code)](/img/ab/066923f1aa1e8dd8dcc572cb60a25d.jpg)
[buucry] sensor (Manchester code)

InfiniBand& RDMA

HNU数据库系统概论 ODBC
随机推荐
Free your hands and automatically brush Tiktok
Apache uses setenvif to identify and release the CDN traffic according to the request header, intercept the DDoS traffic, pay attention to the security issues during CDN deployment, and bypass the CDN
Finger collar pin exclusive Medal
Is it safe for Xiaobai to open a stock account online?
Measurement fitting based on Halcon learning -- Practice [2]
Get the root directory of the package at compile time from buildreoot
Working principle and experimental analysis of DHCP
PHP runtime and memory consumption statistics code
Differences between modems and routers (powercert animated videos)
Pat 1073 scientific notation (20 points) (18 points not finished)
On dynamic programming
[important notice] developer document update (12.13-12.19)
Free cloud function proxy IP pool just released
js (1)
How testers write functional test cases
Win11开始菜单右键空白?Win11开始菜单右键没反应解决方法
启牛证券开户安全嘛?
Docker failed to remotely access 3306 after installing MySQL
CANoe. Diva operation guide TP layer test
Sqlmap for interface security testing