当前位置:网站首页>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》
边栏推荐
- Mutual conversion of CString and char*
- 109 practical shell scripts
- Big end and small end
- Ecu-test report converted to excel format
- Win11录屏数据保存在哪里?Win11录屏数据保存的位置
- Summary of digital image processing knowledge points
- 启牛证券开户安全嘛?
- Understand two major web development patterns
- Canoe learning notes (2)
- OSI notes sorting
猜你喜欢

On ACM competition

Writing manuals using markdown

What is a server? (Powercert animated videos)

Differences between modems and routers (powercert animated videos)

Mathematical analysis_ Notes_ Chapter 4: continuous function classes and other function classes

Local Yum source production

How to write an infinite loop

Canoe learning notes (3)

C language soul torture: do you know the difference between the two?

Canoe learning notes (1)
随机推荐
Free your hands and automatically brush Tiktok
Insert picture in markdown
Website judges network connection disconnection, JS judges network connection disconnection, best practice
Input a line of characters to count the English letters, spaces, numbers and other characters
Docker Alpine image installation PHP extension redis
"Exclusive interview with IDC people" Suzhou Shengwang: the road of IDC transformation in the post epidemic Era
Volatile qualifier
Please enter an integer and output it as several digits, and output each digit in reverse order.
Canoe learning notes (2)
org. apache. ibatis. exceptions. PersistenceException:
【hnu暑学期】数据库系统设计 准备阶段
Big end and small end
The difference between strcpy and memcpy
InfiniBand& RDMA
Ecu-test report converted to excel format
Alicloud disk mounted locally
XMIND to excel test case
MySQL trigger
Command 'GCC' failed with exit status 1 when PIP install mysqlclient
SwiftUI 4 新功能 之 网格视图Gridview组件 (教程含源码)