0

我的代码自 90 年代以来一直在工作,但现在在尝试关闭 Nagle 算法时在 Linux 上被拒绝权限。阅读手册页和谷歌搜索并没有说明原因。有任何想法吗?

  int iFlags, iSize;

  /* NOTE: Sol 2.8 header requires socklen_t but man page says int! */

  int iSizeSize = sizeof( iSize );

#ifdef _WIN32
  unsigned long      ulMSDummy;
  if ( ioctlsocket( iFD, FIONBIO, (u_long FAR*) &ulMSDummy ) != 0 ) {
      printf( "%s: ioctlsocket( %s:%d, FIONBIO, 1 ): %s",
           pszName, pszAddr, iPort, strerror(errno));
      return -1;
  }
#else
  if ( ( iFlags = fcntl( iFD, F_GETFL, 0 ) ) < 0 ) {
      AKWarn( "%s: fcntl( %s:%d, F_GETFL ): %s",
           pszName, pszAddr, iPort, strerror(errno));
      return -1;
  }

  // NOTE: O_NDELAY may need to be changed to FNDELAY on some
  // platforms (which does the same thing) or O_NONBLOCK (which may
  // cause AKread() to return different values when there's no data).
  // Any of these three make the socket non-blocking, which is
  // DIFFERENT from TCP_NODELAY (see below).

  if ( fcntl( iFD, F_SETFL, iFlags | O_NDELAY ) < 0 ) {
      printf( "%s: fcntl( %s:%d, F_SETFL, +NDELAY ): %s",
           pszName, pszAddr, iPort, strerror(errno));
      return -1;
  }
#endif

  // NOTE: TCP_NODELAY is unrelated to the various NDELAY/NONBLOCK
  // options (above).  Instead, it disables the "Nagle Algorithm",
  // which caches tiny packets.

  // NOTE: This option hardcodes a tradeoff for less latency and more
  // packets.  Actually this could be a configuration parameter.

  iFlags = 1;
  if ( setsockopt( iFD, SOL_SOCKET, TCP_NODELAY,
                   (char*) &iFlags, sizeof( int ) ) ) {
      printf( "%s: setsockopt( %s:%d, TCP_NODELAY, %d ): %s",
           pszName, pszAddr, iPort, iFlags, strerror(errno) );
#ifndef __linux__
      return -1; // giving Permission denied on Linux???
#endif
  }
4

1 回答 1

2
 if ( setsockopt( iFD, SOL_SOCKET, TCP_NODELAY,

这从一开始就是错误的。它应该是 IPPROTO_TCP 而不是 SOL_SOCKET。这些是不同的常数。可能它以前从未正常工作过,即做了一些超出您预期的事情。

于 2020-09-17T05:09:09.173 回答