1 29 30 31 package snmp; 32 33 import java.io.*; 34 import java.net.*; 35 36 37 42 43 public class SNMPInformRequestSenderInterface 44 { 45 public static final int SNMP_TRAP_PORT = 162; 46 47 private DatagramSocket dSocket; 48 49 50 53 54 public SNMPInformRequestSenderInterface() 55 throws SocketException 56 { 57 dSocket = new DatagramSocket(); 58 } 59 60 61 62 66 67 public SNMPInformRequestSenderInterface(int localPort) 68 throws SocketException 69 { 70 dSocket = new DatagramSocket(localPort); 71 } 72 73 74 75 79 80 public void sendInformRequest(int version, InetAddress hostAddress, String community, SNMPv2InformRequestPDU pdu) 81 throws IOException 82 { 83 SNMPMessage message = new SNMPMessage(version, community, pdu); 84 85 byte[] messageEncoding = message.getBEREncoding(); 86 87 93 94 DatagramPacket outPacket = new DatagramPacket(messageEncoding, messageEncoding.length, hostAddress, SNMP_TRAP_PORT); 95 96 106 107 dSocket.send(outPacket); 108 109 } 110 111 112 113 117 118 public void sendInformRequest(InetAddress hostAddress, String community, SNMPv2InformRequestPDU pdu) 119 throws IOException 120 { 121 int version = 1; 122 123 sendInformRequest(version, hostAddress, community, pdu); 124 } 125 126 127 128 private String hexByte(byte b) 129 { 130 int pos = b; 131 if (pos < 0) 132 pos += 256; 133 String returnString = new String (); 134 returnString += Integer.toHexString(pos/16); 135 returnString += Integer.toHexString(pos%16); 136 return returnString; 137 } 138 139 140 141 142 143 144 private String getHex(byte theByte) 145 { 146 int b = theByte; 147 148 if (b < 0) 149 b += 256; 150 151 String returnString = new String (Integer.toHexString(b)); 152 153 if (returnString.length()%2 == 1) 155 returnString = "0" + returnString; 156 157 return returnString; 158 } 159 160 161 162 } | Popular Tags |