当前位置:网站首页>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》
边栏推荐
- OSI notes sorting
- PHP runtime and memory consumption statistics code
- JS__ Prototype, prototype chain, call/apply__ Duyi
- [nailing scenario capability package] enterprise and public institution intelligent access control
- Unable to connect to the server remotely locally using the Jupiter notebook
- 银河证券靠谱吗?开证券账户安全吗?
- Command 'GCC' failed with exit status 1 when PIP install mysqlclient
- 启牛学堂证券开户安全嘛?
- HNU数据库系统概论 ODBC
- JS__ Inheritance mode, namespace, object enumeration__ Duyi
猜你喜欢
24 张图一次性说清楚 TCP
InfiniBand& RDMA
Command 'GCC' failed with exit status 1 when PIP install mysqlclient
How testers write functional test cases
IAAs, PAAS, SaaS, baas, FAAS differences
Dbeaver offline installation driver
XMIND to excel test case
Renren mall locates the file according to the route
[nailing scenario capability package] manage the on-the-job / off-the-job situation of employees
Simulate ATM system (account opening, login, account query, withdrawal, deposit, transfer, password modification, account cancellation)
随机推荐
After osx-kvm modifies EFI, oc: failed to load configuration, osx-kvm generates opencore Qcow2 reports an error, libguestfs: error: source '
[nail scenario capability package] hospital visitor verification
Send a more awesome website, which can convert curl commands into code in any language
Alicloud disk mounted locally
The robotframework executes JS commands to move the mouse from X to y
Local Yum source production
Shell syntax
Is Galaxy Securities reliable? Is it safe to open a securities account?
Illustration tcp/ip - Chapter 3 and 4 notes
Finger collar pin exclusive Medal
MySQL trigger
数字图像处理知识点总结概述
PHP runtime and memory consumption statistics code
Canoe learning notes (4)
Modprobe: fatal: module kvmgt not found, kvmgt has no module, kvmgt has no driver, gvt-g precautions, gvt-g precautions for starting win10 in UEFI mode
Data query of server SQL. The most important chapter in database learning
Working principle and experimental analysis of DHCP
The robotframework executes CMD commands and bat scripts
IAAs, PAAS, SaaS, baas, FAAS differences
Bat script simple command