12.1 IP_HDRINCL、SIO_RCVALL没有定义
Q:
编写raw socket程序时需要用到IP_HDRINCL,查阅MSDN后我包含了ws2tcpip.h,编译 时仍报错。
A: scz scz@nsfocus.com
这是一个老问题了,参看:
http://support.microsoft.com/kb/257460
必须在包含winsock2.h之后再包含ws2tcpip.h。类似的情形发生在使用SIO_RCVALL时, 必须在包含winsock2.h之后再包含mstcpip.h。有些人不仔细查看MSDN,就自己定义 了这两个宏,那是不必要的,不推荐这样的编程风格。
/ * For x86/EWindows XP SP1 & VC 7 * cl winsock2_test.c /Fewinsock2_test.exe /nologo /Os /G6 /Gz /Gs65536 /W3 /WX /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /link /RELEASE /
if 0
winsock2_test.exe IP_HDRINCL = 0x00000002 SIO_RCVALL = 0x98000001
endif
include
/ * 必须在包含windows.h之前包含winsock2.h /
include
/ * http://support.microsoft.com/kb/257460 * * ws2tcpip.h must be explicitly included after winsock2.h /
include
/ * mstcpip.h must be explicitly included after winsock2.h /
include
pragma comment( linker, "/INCREMENTAL:NO" )
pragma comment( linker, "/merge:.rdata=.text" )
pragma comment( linker, "/subsystem:console" )
int __cdecl main ( int argc, char * argv[] ) { printf (
"IP_HDRINCL = 0x%08X\n"
"SIO_RCVALL = 0x%08X\n",
IP_HDRINCL,
SIO_RCVALL
);
return( 0 );