CSDN博客

img uuty

About SNMP

发表于2004/10/20 19:51:00  1004人阅读

#include #include int main() { WSADATA data; SOCKET s; sockaddr_in addr; sockaddr_in to; sockaddr_in from; int fromlen = sizeof(from); unsigned char buf[1025]; int ret ; HANDLE hSocketEvent; WSANETWORKEVENTS NetWorkEvents; WSAStartup(MAKEWORD(2,2),&data); s = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); if (s == -1) return 0; addr.sin_family = AF_INET; addr.sin_port = htons(2227); addr.sin_addr.S_un.S_addr = inet_addr("192.168.0.92"); to.sin_family = AF_INET; to.sin_port = htons(161); to.sin_addr.S_un.S_addr = inet_addr("192.168.0.92"); ret = bind(s,(sockaddr*)&addr,sizeof(addr)); if (ret == -1) return 0; buf[0] = 48; buf[1] = 38; //version buf[2] = 0x02; buf[3] = 1; buf[4] = 0; //community name buf[5] = 0x4; //len of community name buf[6] = 6; //community name buf[7] = 'p'; buf[8] = 'u'; buf[9] = 'b'; buf[10] = 'l'; buf[11] = 'i'; buf[12] = 'c'; //GET Request buf[13] = 160; buf[14] = 25; //Id WORD nRand = rand(); memcpy(&buf[15],&rand,2); buf[15] = 0x02; buf[16] = 0x01; buf[17] = 0; //error buf[18] = 0x02; buf[19] = 0x01; buf[20] = 0x00; //index buf[21] = 0x02; buf[22] = 0x01; buf[23] = 0x00; //varbind list buf[24] = 0x30; buf[25] = 14; //var bind buf[26] = 48; buf[27] = 12; //Object ID buf[28] = 6; buf[29] = 8; buf[30] = 43; buf[31] = 0x06; buf[32] = 0x01; buf[33] = 0x02; buf[34] = 0x01; buf[35] = 0x01; buf[36] = 0x04; buf[37] = 0x00; buf[38] = 0x05; buf[39] = 0x00; hSocketEvent = CreateEvent(NULL,TRUE,FALSE,NULL); WSAEventSelect(s,hSocketEvent,FD_READ | FD_CLOSE); int flag = 1; while (1) { printf("Waiting .../n"); ret = sendto(s,(char*)buf,40,0,(sockaddr*)&to,sizeof(to)); DWORD WaitRet = WSAWaitForMultipleEvents(1,&hSocketEvent,FALSE,WSA_INFINITE,TRUE); switch (WaitRet) { case WAIT_OBJECT_0: WSAEnumNetworkEvents(s,hSocketEvent,&NetWorkEvents); if (NetWorkEvents.lNetworkEvents & FD_READ) { if(NetWorkEvents.iErrorCode[FD_READ_BIT]!=0) { continue; } else { memset(buf,0,1025); memset(&from,0,sizeof(from)); ret = recvfrom(s,(char*)buf,1024,0,(sockaddr*)&from,&fromlen); printf("buf = %s , len = %d/n",buf,ret); DWORD dwError = WSAGetLastError(); } } break; default: break; } } WSACleanup(); }
阅读全文
0 0

相关文章推荐

img
取 消
img