我有一些代码,自90年代以来一直没有改变,现在在Linux上试图关闭Nagle算法时却被拒绝了。阅读手册页和谷歌搜索都没有说明原因。有什么想法吗?
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
}发布于 2020-09-17 13:09:09
if ( setsockopt( iFD, SOL_SOCKET, TCP_NODELAY,这从一开始就是错误的。它应该是IPPROTO_TCP而不是SOL_SOCKET。这些是不同的常量。很可能它以前从来没有正常工作过,也就是说,它做了一些超出你预期的事情。
https://stackoverflow.com/questions/63930785
复制相似问题