readn函数功能:在网络编程的读取数据中,通常会需要用到一个读指定字节才返回的函数,linux系统调用中没有给出,需要自己封装。
readn实现代码:
int readn(int fd, void *vptr, size_t n)
{
size_t nleft = n; //readn函数还需要读的字节数
ssize_t nread = 0; //read函数读到的字节数
unsigned char *ptr = (char *)vptr; //指向缓冲区的指针
while (nleft > 0)
{
nread = read(fd, ptr, nleft);
if (-1 == nread)
{
if (EINTR == errno)
nread = 0;
else
return -1;
}
else if (0 == nread)
{
break;
}
nleft -= nread;
ptr += nread;
}
return n - nleft;
}
writen函数功能:读满n个字节才返回
writen代码实现:
int writen(int fd, const void *vptr, size_t n)
{
size_t nleft = n; //writen函数还需要写的字节数
ssize_t nwrite = 0; //write函数本次向fd写的字节数
const char* ptr = vptr; //指向缓冲区的指针
while (nleft > 0)
{
if ((nwrite = write(fd, ptr, nleft)) <= 0)
{
if (nwrite < 0 && EINTR == errno)
nwrite = 0;
else
return -1;
}
nleft -= nwrite;
ptr += nwrite;
}
return n;
}
readline函数功能:读到'\n'或者读满缓冲区才返回
readline函数实现:
static ssize_t readch(int fd, char *ptr)
{
static int count = 0;
static char* read_ptr = 0;
static char read_buf[1024*4] = {0};
if (count <= 0)
{
again:
count = read(fd, read_buf, sizeof(read_buf));
if (-1 == count)
if (EINTR == errno)
goto again;
else
return -1;
else if (0 == count)
return 0;
read_ptr = read_buf;
}
count--;
*ptr = *read_ptr++;
return 1;
}
ssize_t readline(int fd, void *vptr, size_t maxlen)
{
ssize_t i = 0;
ssize_t ret = 0;
char ch = '\0';
char* ptr = NULL;
ptr = (char *)vptr;
for (i = 1; i < maxlen; ++i)
{
ret = readch(fd, &ch);
if (1 == ret)
{
*ptr++ = ch;
if ('\n' == ch)
break;
}
else if (0 == ret)
{
*ptr = 0;
return i-1;
}
else
return -1;
}
*ptr = 0;
return i;
}