做网站流量钱谁给,什么是网络营销4c理论,好网站建设公司地址,爱网盘使用readv和writev函数可以提高数据通信的效率#xff0c;它们的功能可以概括为**“对数据进行整合传输及发送”**。 即使用writev函数可以将分散在多个缓冲中的数据一并发送#xff0c;使用readv函数可以由多个缓冲分别接受#xff0c;所以适当使用他们可以减少I/O函数的调… 使用readv和writev函数可以提高数据通信的效率它们的功能可以概括为**“对数据进行整合传输及发送”**。 即使用writev函数可以将分散在多个缓冲中的数据一并发送使用readv函数可以由多个缓冲分别接受所以适当使用他们可以减少I/O函数的调用次数。 1.readv和writev函数
①readv函数 用于从文件描述符中读取数据并存储在多个缓冲区中。
ssize_t readv(
int fd, //文件描述符。
const struct iovec *iov, //指向 iovec 结构体数组的指针iovec 结构体定义了一个缓冲区。
int iovcnt//iovec 结构体数组的元素个数。
);//iovec 结构体
struct iovec {void *iov_base; //指向缓冲区的起始地址基地址size_t iov_len; //缓冲区的长度即需要传输的字节数
};
示例代码
#includestdio.h
#includesys/uio.h
#define BUF_SIZE 100
int main(int argc, char *argv[])
{struct iovec vec[2];char buf1[BUF_SIZE] {0, };char buf2[BUF_SIZE] {0, };int str_len;//设置两个缓存区第一个存储5个字节剩下的给第二个缓冲区vec[0].iov_base buf1;vec[0].iov_len 5;vec[1].iov_base buf2;vec[1].iov_len BUF_SIZE;str_len readv(0, vec, 2);//第一个参数是0,即接受键盘输入printf(Read total bytes: %d \n, str_len);printf(First message: %s \n, buf1);printf(Second message: %s \n, buf2);return 0;
}②writev()函数 用于将多个缓冲区中的数据写入文件描述符这种方法称为“聚集写”或“向量写”。
ssize_t writev(
int fd, //文件描述符
const struct iovec *iov, //指向 iovec 结构体数组的指针iovec 结构体定义了一个缓冲区。
int iovcnt//iovec 结构体数组的元素个数。
);//iovec 结构体
struct iovec {void *iov_base; //指向缓冲区的起始地址基地址size_t iov_len; //缓冲区的长度即需要传输的字节数
};
示例代码
#includestdio.h
#includesys/uio.hint main(int argc, char *argv[]){struct iovec vec[2];//有两个缓冲char buf1[] 1234567890;char buf2[] ABCDEFGHIJ;int str_len;vec[0].iov_base buf1;vec[0].iov_len 10;vec[1].iov_base buf2;vec[1].iov_len 10;str_len writev(1, vec, 2);//第一个参数是1,所以是向控制台输出puts();printf(writev bytes: %d \n, str_len);
}2.在Windows中实现紧急消息机制
在《TCP/IP网络编程》第十三章多种I/O函数1中已经基于Linux平台实现了MSG_OOB机制但是是基于Linux的信号处理机制所以无法直接移植到Windows平台。
若要在Windows平台上实现该机制则需要通过select()函数该函数简介参考《TCP/IP网络编程》第十二章I/O复用1
PSMSG_OOB在select()监视下会被视为异常数据 发送端代码
#includestdio.h
#includestdlib.h
#includewinsock2.h#define BUFSIZE 30
void ErrorHandling(char* message);int main(int argc, char* argv[]){WSADATA wsa;SOCKET hSocket;SOCKADDR_IN sendAddr;if(argc!3){printf(Usage : %s IP port\n, argv[0]);exit(1);}if(WSAStartup(MAKEWORD(2,2), wsa) ! 0){ErrorHandling(WSAStartup() error!);}hSocket socket(PF_INET, SOCK_STREAM, 0);memset(sendAddr, 0, sizeof(sendAddr));sendAddr.sin_family AF_INET;sendAddr.sin_addr.s_addr inet_addr(argv[1]);sendAddr.sin_port htons(atoi(argv[2]));if(connect(hSocket, (SOCKADDR*)sendAddr, sizeof(sendAddr)) SOCKET_ERROR)ErrorHandling(connect() error!);send(hSocket, 123, 3, 0);send(hSocket, 4, 1, MSG_OOB);//带外数据在select的监视中会被视为异常数据send(hSocket, 567, 3, 0);send(hSocket, 890, 3, MSG_OOB);//只把最后一个字节0作为紧急信息发送closesocket(hSocket);WSACleanup();return 0;
}void ErrorHandling(char* message){fputs(message, stderr);fputc(\n, stderr);exit(1);
}接受端代码
#includestdio.h
#includestdlib.h
#includewinsock2.h#define BUFSIZE 30
void ErrorHandling(char *);int main(int argc, char *argv[]){WSADATA wsa;SOCKET hAcptSock, hRecvSock;SOCKADDR_IN sendAdr, recvAdr;int sendAdrSz,strLen;char buf[BUFSIZE];int result;fd_set read,except,read_copy,except_copy;struct timeval timeout;if(argc ! 2){printf(Usage : %s port\n, argv[0]);exit(1);}if(WSAStartup(MAKEWORD(2,2), wsa) ! 0)ErrorHandling(WSAStartup() error!);hAcptSock socket(PF_INET, SOCK_STREAM, 0);if(hAcptSock INVALID_SOCKET)ErrorHandling(socket() error);memset(recvAdr, 0, sizeof(recvAdr));recvAdr.sin_family AF_INET;recvAdr.sin_addr.s_addr htonl(INADDR_ANY);recvAdr.sin_port htons(atoi(argv[1]));if(bind(hAcptSock, (SOCKADDR*)recvAdr, sizeof(recvAdr)) SOCKET_ERROR)ErrorHandling(bind() error);if(listen(hAcptSock, 5) SOCKET_ERROR)ErrorHandling(listen() error);sendAdrSz sizeof(sendAdr);hRecvSock accept(hAcptSock, (SOCKADDR*)sendAdr, sendAdrSz);FD_ZERO(read); // 清空reads集合FD_ZERO(except); // 清空except集合FD_SET(hRecvSock, read); // 将套接字添加到reads集合FD_SET(hRecvSock, except); // 将套接字添加到except集合while(1){read_copy read;except_copy except;timeout.tv_sec 5;timeout.tv_usec 0;result select(0, read_copy, 0, except_copy, timeout);//开始监视文件描可读数集和与异常集合if(result0){if(FD_ISSET(hRecvSock, except_copy)){//使用FD_ISSET宏检查套接字是否在异常集合中。//如果是表示发生了异常事件此时会调用recv函数并指定MSG_OOB标志来接收带外数据strLen recv(hRecvSock, buf, BUFSIZE-1, MSG_OOB);buf[strLen] 0;printf(urgent message: %s \n, buf);}if(FD_ISSET(hRecvSock, read_copy)){// 如果检测到读事件strLen recv(hRecvSock, buf, BUFSIZE-1, 0);if(strLen 0){break;closesocket(hRecvSock);}else{buf[strLen] 0;printf(normal message: %s, buf);}}}}closesocket(hAcptSock);closesocket(hRecvSock);WSACleanup();return 0;
}void ErrorHandling(char *message){fputs(message, stderr);fputc(\n, stderr);exit(1);
}优先接受MSG_OOB信息4和0。 文章转载自: http://www.morning.gyxwh.cn.gov.cn.gyxwh.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.tfgkq.cn.gov.cn.tfgkq.cn http://www.morning.qcsbs.cn.gov.cn.qcsbs.cn http://www.morning.rrbhy.cn.gov.cn.rrbhy.cn http://www.morning.pprxs.cn.gov.cn.pprxs.cn http://www.morning.plqhb.cn.gov.cn.plqhb.cn http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn http://www.morning.sjsfw.cn.gov.cn.sjsfw.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.wkcl.cn.gov.cn.wkcl.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.rhlhk.cn.gov.cn.rhlhk.cn http://www.morning.ydzly.cn.gov.cn.ydzly.cn http://www.morning.zxfr.cn.gov.cn.zxfr.cn http://www.morning.lxhny.cn.gov.cn.lxhny.cn http://www.morning.zzbwjy.cn.gov.cn.zzbwjy.cn http://www.morning.rkfh.cn.gov.cn.rkfh.cn http://www.morning.txzqf.cn.gov.cn.txzqf.cn http://www.morning.snlxb.cn.gov.cn.snlxb.cn http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.pqnkg.cn.gov.cn.pqnkg.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.zlwg.cn.gov.cn.zlwg.cn http://www.morning.ntcmrn.cn.gov.cn.ntcmrn.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.slfmp.cn.gov.cn.slfmp.cn http://www.morning.fyxr.cn.gov.cn.fyxr.cn http://www.morning.kncrc.cn.gov.cn.kncrc.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.rahllp.com.gov.cn.rahllp.com http://www.morning.wptrm.cn.gov.cn.wptrm.cn http://www.morning.zkbxx.cn.gov.cn.zkbxx.cn http://www.morning.ygwbg.cn.gov.cn.ygwbg.cn http://www.morning.mldrd.cn.gov.cn.mldrd.cn http://www.morning.lhqw.cn.gov.cn.lhqw.cn http://www.morning.snyqb.cn.gov.cn.snyqb.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.mmsf.cn.gov.cn.mmsf.cn http://www.morning.huxinzuche.cn.gov.cn.huxinzuche.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.qdxwf.cn.gov.cn.qdxwf.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn http://www.morning.cyysq.cn.gov.cn.cyysq.cn http://www.morning.znknj.cn.gov.cn.znknj.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.kqzrt.cn.gov.cn.kqzrt.cn http://www.morning.hctgn.cn.gov.cn.hctgn.cn http://www.morning.rrxgx.cn.gov.cn.rrxgx.cn http://www.morning.tpnx.cn.gov.cn.tpnx.cn http://www.morning.sbrrf.cn.gov.cn.sbrrf.cn http://www.morning.mnsts.cn.gov.cn.mnsts.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.rui931.cn.gov.cn.rui931.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn http://www.morning.jmmz.cn.gov.cn.jmmz.cn http://www.morning.wbxr.cn.gov.cn.wbxr.cn http://www.morning.heleyo.com.gov.cn.heleyo.com http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.gczzm.cn.gov.cn.gczzm.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.krdb.cn.gov.cn.krdb.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.dybth.cn.gov.cn.dybth.cn http://www.morning.rkdzm.cn.gov.cn.rkdzm.cn http://www.morning.xykst.cn.gov.cn.xykst.cn