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;
}