作者: ecsun  链接:http://papa.javaeye.com/blog/220559  发表时间: 2008年07月28日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

agent代理端例子:



Java代码
import java.util.*;  
 
import org.snmp4j.*;  
import org.snmp4j.smi.*;  
//agent代理端例子:  
public class Test2 {  
      
    public static class Handler implements org.snmp4j.CommandResponder {  
        protected java.lang.String mAddress = null;  
        protected int mPort = 0;  
        protected java.lang.String mMyCommunityName = null;  
        protected org.snmp4j.TransportMapping mServerSocket = null;  
        protected org.snmp4j.Snmp mSNMP = null;  
 
        public Handler() {  
        }  
 
        public void configure() {  
            mAddress = "192.168.10.191";  
            mPort = 161;  
            mMyCommunityName = "test";  
        }  
 
        public void start() {  
            try {  
                mServerSocket = new org.snmp4j.transport.DefaultUdpTransportMapping(  
                        new org.snmp4j.smi.UdpAddress(java.net.InetAddress  
                                .getByName(mAddress), mPort));  
                mSNMP = new org.snmp4j.Snmp(mServerSocket);  
                mSNMP.addCommandResponder(this);  
                mServerSocket.listen();  
            } catch (java.net.UnknownHostException vException) {  
                System.out.println(vException);  
            } catch (java.io.IOException vException) {  
                System.out.println(vException);  
            }  
        }  
 
        public synchronized void processPdu(  
                org.snmp4j.CommandResponderEvent aEvent) {  
            java.lang.String vCommunityName = new java.lang.String(aEvent  
                    .getSecurityName());  
            System.out.println("Community name " + vCommunityName);  
            org.snmp4j.PDU vPDU = aEvent.getPDU();  
            if (vPDU == null) {  
                System.out.println("Null pdu");  
            } else {  
                System.out.println("(rcv) " + vPDU.toString());  
                switch (vPDU.getType()) {  
                case org.snmp4j.PDU.GET:  
                case org.snmp4j.PDU.GETNEXT:  
                    break;  
                }  
                org.snmp4j.mp.StatusInformation statusInformation = new org.snmp4j.mp.StatusInformation();  
                org.snmp4j.mp.StateReference ref = aEvent.getStateReference();  
                try {  
                    System.out.println("Sending response");  
                    vPDU.setType(PDU.RESPONSE);  
                    vPDU.set(0, new VariableBinding(vPDU.get(0).getOid(),  
                            new OctetString("Test")));  
 
                    aEvent.getMessageDispatcher().returnResponsePdu(  
                            aEvent.getMessageProcessingModel(),  
 
                            aEvent.getSecurityModel(),  
                            aEvent.getSecurityName(),  
 
                            aEvent.getSecurityLevel(), vPDU,  
                            aEvent.getMaxSizeResponsePDU(), ref,  
 
                            statusInformation);  
                } catch (org.snmp4j.MessageException vException) {  
                    System.out.println(vException);  
                }  
            }  
        }  
    }  
 
    public static void main(String argv[]) {  
        Handler h = new Handler();  
        h.configure();  
        h.start();  
        /** Do nothing loop * */ 
        while (true) {  
            synchronized (Test2.class) {  
                try {  
                    Test2.class.wait();  
                } catch (Exception e) {  
                }  
            }  
        }  
    }  
}

已有 0 人发表留言,猛击->>这里<<-参与讨论


JavaEye推荐