写入管道时区分缓冲区溢出和其他错误

时间:2017-03-05 19:13:29

标签: c linux pipe

我正在写一个管道,并得到一个

"Buffer overflow? : Resource temporarily unavailable"错误。

我想区分缓冲区溢出错误和其他与管道相关的错误。

以下是我用于写入管道的代码:

void forward_data_asynch(int source_sock, int destination_sock) {
  char buffer[BUF_SIZE];
  int n;

  //put in error condition for -1, currently the socket is shutdown
  while ((n = recv(source_sock, buffer, BUF_SIZE, 0)) > 0)// read data from input socket 
    { 
      send(destination_sock, buffer, n, 0); // send data to output socket
      if( write(pfds[1],buffer,n) < 0 )//send data to pipe
          perror("Buffer overflow? ");
    }

  shutdown(destination_sock, SHUT_RDWR); 
  close(destination_sock);

  shutdown(source_sock, SHUT_RDWR);
  close(source_sock);
}

管道已设置为非阻塞:

/**   Make file descriptor non blocking */
int setNonblocking(int fd)
{
  int flags;

  /* If they have O_NONBLOCK, use the Posix way to do it */
#if defined(O_NONBLOCK)
  /* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */
  if (-1 == (flags = fcntl(fd, F_GETFL, 0)))
    flags = 0;
  return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
#else
  /* Otherwise, use the old way of doing it */
  flags = 1;
  return ioctl(fd, FIOBIO, &flags);
#endif
}   

0 个答案:

没有答案