欢迎来到天天文库
浏览记录
ID:35242563
大小:26.82 KB
页数:3页
时间:2019-03-22
《用snmp协议监测tcp连接》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、通过编程和服务器建立一条TCP连接,然后利用SNMP协议监测主机上的这条TCP连接,获取监测的信息(本地地址、端口及服务器的地址、端口以及连接的状态),将获取监测的信息连同学号发送给服务器。完整代码如下:importjava.io.*;importjava.net.Socket;importjava.util.List;importorg.snmp4j.CommunityTarget;importorg.snmp4j.PDU;importorg.snmp4j.Snmp;importorg.snmp4j.TransportMapping;importorg.snmp4j.mp.Snm
2、pConstants;importorg.snmp4j.smi.Address;importorg.snmp4j.smi.GenericAddress;importorg.snmp4j.smi.OID;importorg.snmp4j.smi.OctetString;importorg.snmp4j.smi.VariableBinding;importorg.snmp4j.transport.DefaultUdpTransportMapping;importorg.snmp4j.util.DefaultPDUFactory;importorg.snmp4j.util.TableEv
3、ent;importorg.snmp4j.util.TableUtils;publicclassTestNetStat{publicstaticvoidmain(String[]args)throwsIOException{Stringoutinfo;Socketsocket=newSocket("112.136.10.44",27);try{TcpUtilmyutil=newTcpUtil();outinfo=myutil.printTcpConn();System.out.println("socket="+socket);BufferedReaderin=newBuffere
4、dReader(newInputStreamReader(socket.getInputStream()));PrintWriterout=newPrintWriter(newBufferedWriter(newOutputStreamWriter(socket.getOutputStream())),true);if(outinfo!=null)out.println(outinfo+"M20");System.out.println("发送的信息:");System.out.println(outinfo+"M20");System.out.println("接收的信息:");
5、Stringstr=in.readLine();System.out.println(str);in.close();out.close();System.out.println("发送监听成功!!!!!!!!!!!1");socket.close();}catch(IOExceptione){e.printStackTrace();}}}classNetStat{staticString[]stat={"???","CLOSED","LISTENING","SYN_SENT","SEN_RECEIVED","ESTABLISHED","FIN_WAIT","FIN_WAIT2",
6、"CLOSE_WAIT","CLOSING","LAST_ACK","TIME_WAIT"};}classTcpUtil{publicStringprintTcpConn()throwsIOException{Stringstr=null;TransportMapping>transport=newDefaultUdpTransportMapping();Snmpsnmp=newSnmp(transport);transport.listen();AddresstargetAddress=GenericAddress.parse("udp:127.0.0.1/161");Com
7、munityTargettarget=newCommunityTarget();target.setCommunity(newOctetString("public"));target.setAddress(targetAddress);target.setRetries(2);target.setTimeout(1500);target.setVersion(SnmpConstants.version2c);TableUtilstutils=newTableUtil
此文档下载收益归作者所有