当前位置:网站首页>Preliminary solution of i/o in socket programming
Preliminary solution of i/o in socket programming
2022-06-25 21:42:00 【ypd.】
SOCKET Programming part I/O Preliminary solution of
What is part of a socket I/O
- in many instances , When executing on a streaming socket
I/OIt will appear during operationPartial readingandPartial writeThe phenomenon of - perform
read()andwrite()When the system is called , In some cases , There may be Transmitted data Less than Requested data
When will this phenomenon occur
- read():
- Available data ratio read() Less data requested , here read() Returns the number of bytes available
- write():
- write() After transmitting some requested bytes, it is interrupted by the signal processing process
- Socket works in non blocking mode (
O_NONBLOCK),ProbablyOnly part of the transmission - An asynchronous error occurred after some of the requested bytes had been transferred
How to solve
- The system call can be called again to complete the transmission of all data
- We are right.
read()Andwrite()Construct their respective wrapping functions , It can call the corresponding system call circularly , Ensure that the requested number of bytes can always be fully transmitted
- Unless If something goes wrong or read() Detected. EOF
SHOW ME THE CODE
- readn()
/* readn() The parameters of read() identical It's recycled read() system call Ensure that the requested number of bytes is always fully transmitted 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 Type is only for processing read return -1 The situation of , Nothing else 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() The parameters of write() identical It's recycled write() system call Ensure that the requested number of bytes is always fully transmitted 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
}
Reference resources
- 《TLPI》
边栏推荐
- PHP runtime and memory consumption statistics code
- Free cloud function proxy IP pool just released
- Circular structure and circular keywords
- idea怎么把自己的项目打包成jar包
- InfiniBand& RDMA
- Pat 1083 list grades (25 points)
- Concept and understanding of variables
- Build the first website with idea
- 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
- MySQL is slow to add indexes_ Why is your SQL so slow? Why is your MySQL index invalid?
猜你喜欢

Summary of several methods for FPS calculation

Top in the whole network, it is no exaggeration to say that this Stanford machine learning tutorial in Chinese notes can help you learn from the beginning to the mastery of machine learning
![[nail scenario capability package] hospital visitor verification](/img/0e/43433ca5586c48d01708e5fa39a808.jpg)
[nail scenario capability package] hospital visitor verification

Simple record of fire & spell effects

Win11开始菜单右键空白?Win11开始菜单右键没反应解决方法

Canoe learning notes (4)

CANoe. Diva operation guide TP layer test

Win11录屏数据保存在哪里?Win11录屏数据保存的位置

Canoe learning notes (2)

Mathematical analysis_ Notes_ Chapter 4: continuous function classes and other function classes
随机推荐
IAAs, PAAS, SaaS, baas, FAAS differences
智云健康上市在即:长期亏损,美年健康俞熔已退出,未来难言乐观
【Proteus仿真】Arduino UNO花样流水灯
[important notice] developer document update (12.13-12.19)
【Proteus仿真】Arduino UNO+按键控制2位数码管倒计时
CANoe. Diva operation guide TP layer test
[nailing - scenario capability package] nailer card
. Thoughts on software trends in the 20th anniversary of net
[nailing scenario capability package] company / Park Digital canteen
Insert picture in markdown
熊市指南|一些本质的教训与具体的生存法则
Mathematical analysis_ Notes_ Chapter 4: continuous function classes and other function classes
Common singleton functions traverse dictionary functions
Local Yum source production
Circular structure and circular keywords
XMIND to excel test case
Jmeter- (III) create user test cases for interface testing
InfiniBand& RDMA
Windows11 windows security center cannot open Windows Defender cannot open
Is it safe to fry stocks with compass or securities software? Where can I open an account and how