问题
I have downloaded SNMP4J and I added it to buildpath of my project. Then I added some codes from http://shivasoft.in/blog/java/snmp/create-snmp-client-in-java-using-snmp4j/ and http://shivasoft.in/blog/java/snmp/creating-snmp-agent-server-in-java-using-snmp4j/ to check my agent. But now These strings printed in console:
Exception in thread "main" java.lang.RuntimeException: java.net.BindException: Address already in use: Cannot bind
at org.snmp4j.transport.TransportMappings.createTransportMapping(TransportMappings.java:123)
at org.bihe.SNMPAgent.initTransportMappings(SNMPAgent.java:145)
at org.snmp4j.agent.BaseAgent.init(BaseAgent.java:144)
at org.bihe.SNMPAgent.start(SNMPAgent.java:157)
at org.bihe.TestSNMPAgent.init(TestSNMPAgent.java:35)
at org.bihe.TestSNMPAgent.main(TestSNMPAgent.java:13)
Caused by: java.net.BindException: Address already in use: Cannot bind
at java.net.DualStackPlainDatagramSocketImpl.socketBind(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.bind0(Unknown Source)
at java.net.AbstractPlainDatagramSocketImpl.bind(Unknown Source)
at java.net.DatagramSocket.bind(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at org.snmp4j.transport.DefaultUdpTransportMapping.<init>(DefaultUdpTransportMapping.java:100)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.snmp4j.transport.TransportMappings.createTransportMapping(TransportMappings.java:104)
... 5 more
I uploaded my hole project in mediafire: https://www.mediafire.com/?lcyap63rs93qdwo can anyone explain me, why the program throws these exceptions? I have not worked with snmp before and it is my first experience for a research in a network course.
回答1:
You are trying to listen on a local IP and port which is already in use by some other process (for example the operating system - if you use port 161 this is rather likely).
Try to use a different port (or IP address - but most services listen on all local IP addresses) or stop the process that is using it.
回答2:
As noted by ooSNMP, some process is already using the port. Either use a different port or stop the process.
Windows: There could be a service: SNMPT Trap on that was automatically listening for traps. If you are on Windows this may be the issue. This could be fixed by going to Services and stopping the service. Make sure that you set the service to "manual", or it will eventually restart itself (this is done by right clicking on the service, selecting Properties and changing Startup Type.
Linux: You can find out about which application is using the port using the ss command. You can kill the process using the kill command.
来源:https://stackoverflow.com/questions/20975575/runtimeexception-in-snmp4j-agent