RT~~~~部分代码如下:// 初始化SOCKET
WSADATA wsaData;
int iErrorCode = ::WSAStartup( MAKEWORD(2,1), &wsaData );
if( SOCKET_ERROR == iErrorCode )
{
printf( "WSAStartup() error. " );
return -1;
} SOCKET sock = ::socket( AF_INET, SOCK_RAW, IPPROTO_IP );
if( INVALID_SOCKET == sock )
{
printf( "socket() error. " );
return -1;
} //获取本机IP地址
char szHostName[200];
iErrorCode = ::gethostname( szHostName, sizeof(szHostName) );
if( SOCKET_ERROR == iErrorCode )
{
printf( "gethostname() error. " );
return -1;
} PHOSTENT pHostent = ::gethostbyname( szHostName ); if( NULL == pHostent )
{
printf( "gethostbyname() error. " );
return -1;
} SOCKADDR_IN sa;
sa.sin_family = AF_INET;
sa.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //inet_addr("192.168.1.101"); //设定绑定网络的ip,未来需要手动传入ip
sa.sin_port = htons( 60000 ); iErrorCode = ::bind( sock, (PSOCKADDR)&sa, sizeof(sa) );
if( SOCKET_ERROR == iErrorCode )
{
printf( "bind() error. " );
return -1;
} // 设置 SOCK_RAW 为 SIO_RCVALL,接收所有的 IP 包
DWORD dwBufferLen[10];
DWORD dwBufferInLen = 1;
DWORD dwBytesReturned = 0;
iErrorCode = ::WSAIoctl( sock, SIO_RCVALL, &dwBufferInLen, sizeof(dwBufferInLen), &dwBufferLen,
sizeof( dwBufferLen ), &dwBytesReturned, NULL, NULL );
if( SOCKET_ERROR == iErrorCode )
{
printf( "Ioctl() error. " );
return -1;
} //侦听IP报文
while( 1 )
{
char package[8 * 1024] = { 0 };    // 数据缓冲区
iErrorCode = ::recv( sock, package, sizeof(package), 0 );

if( SOCKET_ERROR == iErrorCode )
{
printf( "recv() error. " );
}