I have created a SNMP agent simulator application which use the port number 161 for the simulated devices. sometimes it occurs the port alredy in use exception. how can i know if the 161 port is busy or not?
By using netstat command.
Specifically,
netstat -s [PORT_NO]
For example,
netstat -s 161
By just starting your application. If it gets a BindException, the port is in use. If it doesn't, your application can run. Knowing that ahead of time doesn't really buy you anything.
Just additional remark. I think you may be forbidden to use low port numbers (security policy if not a root user) - try using higher numbers i.e. 10161.
1)ps -efww | grep 161
2)netstat -anp | grep 161
On Windows you can use netstat
and tasklist
. For example,
netstat -aon | findstr 161
Its output should be
C:\Program Files\Microsoft Visual Studio 9.0\VC>netstat -aon |findstr 161
UDP 0.0.0.0:161 *:* 1620
UDP [::]:161 *:* 1620
Then use tasklist
tasklist /fi "PID eq 1620"
Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
snmp.exe 1620 Services 0 1,172 K
In my case obviously it is Windows SNMP services who uses port 161. In your case, it may be another process.
following statement works for me.
netstat -lnp
sample usage:
appuser@-app:~$ sudo netstat -lnp |grep 162
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1162/mysqld
udp6 0 0 127.0.0.1:162 :::* 6830/java
来源:https://stackoverflow.com/questions/12739768/snmp-error-with-port-already-in-use