Home > System Tutorial > LINUX > body text

readn, writen and readline functions in Linux network programming: how to read and write byte streams efficiently

王林
Release: 2024-02-14 13:00:03
forward
922 people have browsed it

在Linux网络编程中,我们经常需要使用字节流套接字来进行数据的传输和接收。但是,字节流套接字上调用read和write函数时,输入或输出的字节数可能比请求的数量少,因为内核中用于套接字的缓冲区是有限制的,需要调用者多次调用read或write函数。这样会增加编程的复杂度和出错的可能性。为了解决这个问题,我们可以使用一些封装好的函数来实现固定长度或按行读写的功能,这些函数就是readn、writen和readline函数。本文将介绍这些函数的作用、原理、实现和使用方法,帮助你提高Linux网络编程的效率和可靠性。

readn, writen and readline functions in Linux network programming: how to read and write byte streams efficiently

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;
}
Copy after login

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)) if (nwrite else
        return -1;
    }
    nleft -= nwrite;
    ptr += nwrite;
  }
  return n;
}
Copy after login

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 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 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;
}
Copy after login

通过本文,你应该对readn、writen和readline函数有了一个基本的了解,它们是对read和write函数的封装,可以实现固定长度或按行读写的功能,避免了字节流套接字上读写时可能出现的不足或错误。当然,这些函数也不是完美无缺的,它们有一些局限性和注意事项,比如对于非阻塞套接字、多线程环境、信号处理等情况,需要进行额外的处理或修改。总之,readn、writen和readline函数是Linux网络编程中非常有用的工具,值得你深入学习和掌握。

The above is the detailed content of readn, writen and readline functions in Linux network programming: how to read and write byte streams efficiently. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!