标签:
ssize_t readn(int fd, void *buf, size_t count) ssize_t writen(int fd, void *buf, size_t count)
read
、write
一致。它们的作用的读取/写入count个字节后再返回。其实现如下:
ssize_t readn(int fd, void *buf, size_t count) { int left = count ; //剩下的字节 char * ptr = (char*)buf ; while(left>0) { int readBytes = read(fd,ptr,left); if(readBytes< 0)//read函数小于0有两种情况:1中断 2出错 { if(errno == EINTR)//读被中断 { continue; } return -1; } if(readBytes == 0)//读到了EOF { //对方关闭呀 printf("peer close\n"); return count - left; } left -= readBytes; ptr += readBytes ; } return count ; } /* writen 函数 写入count字节的数据 */ ssize_t writen(int fd, void *buf, size_t count) { int left = count ; char * ptr = (char *)buf; while(left >0) { int writeBytes = write(fd,ptr,left); if(writeBytes<0) { if(errno == EINTR) continue; return -1; } else if(writeBytes == 0) continue; left -= writeBytes; ptr += writeBytes; } return count; }
有了这两个函数之后,我们就可以使用定长包来发送数据了,我抽取其关键代码来讲诉:
char readbuf[512]; readn(conn,readbuf,sizeof(readbuf)); //每次读取512个字节
同理的,写入的时候也写入512个字节
char writebuf[512];
fgets(writebuf,sizeof(writebuf),stdin);
writen(conn,writebuf,sizeof(writebuf);
struct packet { unsigned int msgLen ; //4个字节字段,说明数据部分的大小 char data[512] ; //数据部分 }
//发送数据过程 struct packet writebuf; memset(&writebuf,0,sizeof(writebuf)); while(fgets(writebuf.data,sizeof(writebuf.data),stdin)!=NULL) { int n = strlen(writebuf.data); //计算要发送的数据的字节数 writebuf.msgLen =htonl(n); //将该字节数保存在msgLen字段,注意字节序的转换 writen(conn,&writebuf,4+n); //发送数据,数据长度为4个字节的msgLen 加上data长度 memset(&writebuf,0,sizeof(writebuf)); }
memset(&readbuf,0,sizeof(readbuf)); int ret = readn(conn,&readbuf.msgLen,4); //先读取四个字节,确定后续数据的长度 if(ret == -1) { err_exit("readn"); } else if(ret == 0) { printf("peer close\n"); break; } int dataBytes = ntohl(readbuf.msgLen); //字节序的转换 int readBytes = readn(conn,readbuf.data,dataBytes); //读取出后续的数据 if(readBytes == 0) { printf("peer close\n"); break; } if(readBytes<0) { err_exit("read"); }
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
与read函数相比,recv函数的区别在于两点:
recv函数的flags参数常用的选项是:
为了实现按行读取,我们需要使用recv函数的MSG_PEEK选项。PEEK的意思是"偷看",我们可以理解为窥视,看看socket的缓冲区内是否有某种内容,而清除缓冲区。
/* * 封装了recv函数 返回值说明:-1 读取出错 */ ssize_t read_peek(int sockfd,void *buf ,size_t len) { while(1) { //从缓冲区中读取,但不清除缓冲区 int ret = recv(sockfd,buf,len,MSG_PEEK); if(ret == -1 && errno == EINTR)//文件读取中断 continue; return ret; } } 下面是按行读取的代码: /* *读取一行内容 * 返回值说明: == 0 :对端关闭 == -1 : 读取错误 其他:一行的字节数,包含\n * **/ ssize_t readLine(int sockfd ,void * buf ,size_t maxline) { int ret ; int nRead = 0; int left = maxline ; char * pbuf = (char *) buf; int count = 0; while(true) { //从socket缓冲区中读取指定长度的内容,但并不删除 ret = read_peek(sockfd,pbuf,left); // ret = recv(sockfd , pbuf , left , MSG_PEEK); if(ret<= 0) return ret; nRead = ret ; for(int i = 0 ;i< nRead ; ++i) { if(pbuf[i]==‘\n‘) //探测到有\n { ret = readn (sockfd , pbuf, i+1); if(ret != i+1) exit(EXIT_FAILURE); return ret + returnCount; } } //如果嗅探到没有\n //那么先将这一段没有\n的读取出来 ret = readn(sockfd , pbuf , nRead); if(ret != nRead) exit(EXIT_FAILURE); pbuf += nRead ; left -= nRead ; count += nRead; } return -1; }
标签:
原文地址:http://www.cnblogs.com/Kobe10/p/5770977.html