Submitted by Iceberg On 2004, October 9, 7:13 AM. C / C 1. Development Destination and Principle ---- The company needs to study the interior of multiple Ethernet switches (also known as smart hubs) during product development The network management information structure, for this purpose, a "eavesdrop" program must be written, and the network management program and the communication content of the switch are analyzed. I use the MFC Socket class in Visual C 6.0 to successfully implement the above purposes. ---- Currently, standard network management procedures and network devices support network management network devices communicate with standard Simple Network Management Protocol (SNMP). SNMP is a high-level protocol based on UDP / IP. Communication parties are transmitted by various network management information and control information according to SNMP, and can perform event real-time reports or alarms, so that network administrators can easily control the current operation of the network in a timely manner. ---- The range of network management information is very wide, such as network traffic, connection status, etc., due to the different management equipment, manufacturers can also customize network management information of their own products in accordance with relevant international standards. The NMS information is defined in the Management Information Library (MIB), and the entire system is an extensible tree structure. The network management information of one article is packaged in the SNMP protocol package, and then transmits the transfer layer to the UDP package, and then transmits it through the Socket mechanism. ---- The basic principles of this program are: inserted to "bullying" between the network management procedures and the managed equipment, posing as a tube device when communicating with the network management program; pushing the network management program when communicating with the managed equipment The program "None Talks"; this procedure is secretly recorded in order to record the content, and then "upload", will forward the content to the real recipient, so that the conversation will continue, so it is. Second, programming ideas and specific processes ---- This program does not require a complex graphical interface, so simply use Project Wizard to open a MFC application that supports Socket and dialog-based MFC applications. The class named cchatdlg is named cChatdlg, and then use the resource editor to add a button on this dialog, and the text is "listen". All of the received information will be played in a TRACE statement in the Output window of the Visual C integration environment. The purpose of this is to facilitate seeing various data in a timely manner, and of course other methods can be used. In this program, the client refers to the network management program and the Server refers to the switch. ---- Then add two classes to this item, which are derived from CSocket, and can call the ClassWizard tool to generate. CClientSocket is used to receive UDP data from the network management program, and Cserversocket is used to receive UDP data from the switch.
These two classes are defined as follows: class CClientSocket: public CSocket {// Attributes public: // Operations public: CClientSocket (CChatDlg * pdlg); virtual ~ CClientSocket (); // Overrides public: BOOL m_bFirst; CChatDlg * pDlg; / / ClassWizard generated virtual function overrides // {{AFX_VIRTUAL (CClientSocket) public: virtual void OnReceive (int nErrorCode); //}} AFX_VIRTUAL // Generated message map functions // {{AFX_MSG (CClientSocket) // NOTE - the ClassWizard will Add and remover functions here. //}} AFX_MSG // Implement Protected:}; m_bfirst and PDLG are customized two categories, which are shown here. class CServerSocket: public CSocket {// Attributes public: // Operations public: CServerSocket (CChatDlg * pdlg); virtual ~ CServerSocket (); // Overrides public: CChatDlg * pDlg; // ClassWizard generated virtual function overrides // {{AFX_VIRTUAL (CServerSocket) public: virtual void OnReceive (int nErrorCode); //}} AFX_VIRTUAL // Generated message map functions // {{AFX_MSG (CServerSocket) // NOTE - the ClassWizard will add and remove member functions here //}}. AFX_MSG // Implementation Protected:}; then add the handle of the button Listen in the cchatdlg class is as follows: void cchatdlg :: OnListen () {pClientSocket = new cclientsocket (this); if (pclientSocket! = Null) {= pClientSocket-> Create (SNMP_SOCKET_PORT, SOCK_DGRAM)) AfxMessageBox ( "Can not create ClientSocket!"); else :: EnableWindow (GetDlgItem (IDC_LISTEN) -> m_hWnd, FALSE);} else {AfxMessageBox () "Can not new ClientSocket!" }} Note: SNMP_Socket_Port should be set to 161.
Then, in the virtual function CClientSocket OnReceive implements Content: void CClientSocket :: OnReceive (int nErrorCode) {CSocket :: OnReceive (nErrorCode); unsigned char tmp [MAXTMPSIZE]; // MAXTMPSIZE is custom macros, may 1024 ; int i; int RecNum; UINT ClientPort; CString clientAddress; if (m_bFirst) {m_bFirst = false; RecNum = ReceiveFrom (tmp, MAXTMPSIZE, clientAddress, ClientPort); if (RecNum> 0) {TRACE ( "Received from client,% D bytes: / n ", recnum); for (i = 0; i
Send the contents and functions ---- createServerSocket upper program CChatDlg follows: void CChatDlg :: CreateServerSocket (CString address, UINT port) {m_ClientAddress = address; m_ClientPort = port; pServerSocket = new CServerSocket (this); if (pServerSocket! = NULL) {if (! pServerSocket-> Create (m_ClientPort, SOCK_DGRAM)) AfxMessageBox ( "Can not create ServerSocket!");} else AfxMessageBox () "Can not new ServerSocket!";} void CChatDlg :: Send (BOOL ToServer , unsigned char * buf, int buf_len) {if (ToServer) {if (pServerSocket = NULL) {if (pServerSocket-> SendTo (buf, buf_len, SNMP_SOCKET_PORT, m_ServerAddress!) == SOCKET_ERROR) AfxMessageBox ( "Error: fail to send data to server ");}} else {if (pClientSocket = NULL) {if (pClientSocket-> SendTo (buf, buf_len, m_ClientPort, m_ClientAddress!) == SOCKET_ERROR) AfxMessageBox (" Error:! fail to send data to client! ");}}} ---- Note: m_serveraddress is the IP address of the switch, which is set in advancedlg's OnInitDialog function or elsewhere. ---- Finally, to process the received UDP packet from the switch, display the data in the Output window, and then call the CCHATDLG's Send function to forward it to the network management program.