setsockopt( TCP_NODELAY, 1 ): 权限被拒绝

setsockopt( TCP_NODELAY, 1 ): Permission denied

我的代码自 90 年代以来一直没有更改,现在在尝试关闭 Nagle 算法时 Linux 上的权限被拒绝。阅读手册页和 google 搜索没有说明原因。有什么想法吗?

  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
  }
 if ( setsockopt( iFD, SOL_SOCKET, TCP_NODELAY,

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