源代码是这样的:
#define WSA_FLAG_OVERLAPPED 0x01
#define _WSAIOW(x,y) (IOC_IN|(x)|(y))
#define IOC_VENDOR 0x18000000
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD( 2, 2 );
if ( WSAStartup( wVersionRequested, &wsaData ) != 0 )
{
return FALSE;
}
SOCKET sock ;
//sock = WSASocket(AF_INET, SOCK_RAW, IPPROTO_IP, NULL, 0, WSA_FLAG_OVERLAPPED);
sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
if( INVALID_SOCKET == sock )
{
AfxMessageBox(L"socket() failed!");
closesocket( sock ) ;
return FALSE;
}
AfxMessageBox(L"socket() success!");
int rcvtimeo = 5000 ; // 5 sec instead of 45 as default
if( setsockopt( sock , SOL_SOCKET , SO_RCVTIMEO , (const char *)&rcvtimeo , sizeof(rcvtimeo) ) == SOCKET_ERROR)
{
closesocket( sock ) ;
return FALSE ;
}
SOCKADDR_IN sa;
DWORD HostNetOrderIP = GetHostNetOrderIP();
if( HostNetOrderIP == 0 )
return FALSE;
sa.sin_family = AF_INET;
sa.sin_port = htons(161);
sa.sin_addr.s_addr= HostNetOrderIP;
if (bind(sock, (PSOCKADDR)&sa, sizeof(sa)) == SOCKET_ERROR)
{
closesocket( sock ) ;
return FALSE;
}
DWORD dwBufferLen[10] ;
DWORD dwBufferInLen= 1 ;
DWORD dwBytesReturned = 0 ;
if( SOCKET_ERROR == WSAIoctl( sock, SIO_RCVALL , &dwBufferInLen,
sizeof(dwBufferInLen), &dwBufferLen, sizeof(dwBufferLen),
&dwBytesReturned , NULL , NULL ) )
{
closesocket( sock ) ;
return FALSE;
}
#include
//#include
包含#include 头文件编译通不过
使用#include 头文件编译可以通过,但是sock创建失败!
请各位大大帮我看看,谢谢了!
我做了个HID和CDC的项目,给你参考一下
const uint8_t CDCHID_ConfigDescriptor[CDCHID_SIZ_CONFIG_DESC] =
{
0x09, /* bLength: Configuation Descriptor size */
USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
CDCHID_SIZ_CONFIG_DESC,
/* wTotalLength: Bytes returned */
0x00,
0x03, /* bNumInterfaces: 1 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor describing
the configuration*/
0xC0, /* bmAttributes: Bus powered */
0x32, /* MaxPower 100 mA: this current is used for detecting Vbus */
/* 09 */
//USB CDC descriptor
/*USB Interface Association Descriptor */
0x08,
USB_INTERFACE_ASSOCIATION_TYPE,
0x00,// EPs used in CDC/ACM Function.
0x02,
0x02,// Interface class code for a CDC communication class interface.
0x02,// Interface subclass code for an Abstract Control Model interface descriptor.
0x00,// implemenent any particular protocol.
0x00,// No string descriptor for this interface
/* 17 */
/*Interface Descriptor*/
0x09, /* bLength: Interface Descriptor size */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: Interface */
/* Interface descriptor type */
0x00, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x01, /* bNumEndpoints: One endpoints used */
0x02, /* bInterfaceClass: Communication Interface Class */
0x02, /* bInterfaceSubClass: Abstract Control Model */
0x00, /* bInterfaceProtocol: Common AT commands */
0x00, /* iInterface: */
/* 26 */
/*Header Functional Descriptor*/
0x05, /* bLength: Endpoint Descriptor size */
0x24, /* bDescriptorType: CS_INTERFACE */
0x00, /* bDescriptorSubtype: Header Func Desc */
0x10, /* bcdCDC: spec release number */
0x01,
/* 31 */
/*Call Managment Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x01, /* bDescriptorSubtype: Call Management Func Desc */
0x00, /* bmCapabilities: D0+D1 */
0x01, /* bDataInterface: 1 */
/* 36 */
/*ACM Functional Descriptor*/
0x04, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x02, /* bDescriptorSubtype: Abstract Control Management desc */
0x02, /* bmCapabilities */
/* 40 */
/*Union Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x06, /* bDescriptorSubtype: Union func desc */
0x00, /* bMasterInterface: Communication class interface */
0x01, /* bSlaveInterface0: Data Class Interface */
/* 45 */
/*Endpoint 2 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
0x82, /* bEndpointAddress: (IN2) */
0x03, /* bmAttributes: Interrupt */
VIRTUAL_COM_PORT_INT_SIZE, /* wMaxPacketSize: */
0x00,
0xFF, /* bInterval: */
/* 52 */
/*Data class interface descriptor*/
0x09, /* bLength: Endpoint Descriptor size */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: */
0x01, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints: Two endpoints used */
0x0A, /* bInterfaceClass: CDC */
0x00, /* bInterfaceSubClass: */
0x00, /* bInterfaceProtocol: */
0x00, /* iInterface: */
/* 61 */
/*Endpoint 3 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
0x03, /* bEndpointAddress: (OUT3) */
0x02, /* bmAttributes: Bulk */
VIRTUAL_COM_PORT_DATA_SIZE, /* wMaxPacketSize: */
0x00,
0x00, /* bInterval: ignore for Bulk transfer */
/* 68 */
/*Endpoint 1 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
0x84, /* bEndpointAddress: (IN1) */
0x02, /* bmAttributes: Bulk */
VIRTUAL_COM_PORT_DATA_SIZE, /* wMaxPacketSize: */
0x00,
0x00, /* bInterval */
/* 75 */
//end of CDC
/************** Descriptor of CDC HID interface ****************/
0x09, /* bLength: Interface Descriptor size */
USB_INTERFACE_DESCRIPTOR_TYPE,/* bDescriptorType: Interface descriptor type */
0x02, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints */
0x03, /* bInterfaceClass: HID */
0x00, /* bInterfaceSubClass : 1=BOOT, 0=no boot */
0x00, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */
0, /* iInterface: Index of string descriptor */
/* 84 */
/******************** Descriptor of CDC HID HID ********************/
0x09, /* bLength: HID Descriptor size */
HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */
0x10, /* bcdHID: HID Class Spec release number */
0x01,
0x00, /* bCountryCode: Hardware target country */
0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
0x22, /* bDescriptorType */
CDCHID_SIZ_REPORT_DESC,/* wItemLength: Total length of Report descriptor */
0x00,
/* 93 */
/******************** Descriptor of CDC HID endpoints ******************/
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
0x81, /* bEndpointAddress: Endpoint Address (IN) */
0x03, /* bmAttributes: Interrupt endpoint */
0x40, /* wMaxPacketSize: 2 Bytes max */
0x00,
0x20, /* bInterval: Polling Interval (32 ms) */
/* 100 */
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: */
/* Endpoint descriptor type */
0x01, /* bEndpointAddress: */
/* Endpoint Address (OUT) */
0x03, /* bmAttributes: Interrupt endpoint */
0x08, /* wMaxPacketSize: 2 Bytes max */
0x00,
0x20, /* bInterval: Polling Interval (20 ms) */
/* 107 */
}