SNMP++ Non-Default Port Number Connections

I am attempting to connect to a device that is using a non-standard port number with SNMP (501) using the SNMP++ library. When I create the Snmp object using any port other than 161, I get timeout errors. However, I am still able to use the netsnmp snmp_get using port 501 successfully.

Am I missing something with setting the port number? Does SNMP++ only support port 161?

Thank you!


snmp++ does not have any special restrictions on the ports. You could use the snmpNext example to access the device. If SNMPv1 with public community is configured, the command “./snmpNext -P501” should work.

Some more details:

  • When constructing a Snmp object, you should pass “0” as the port number, so a (random) free client port will be used. This is OK for your use case, as this port is used as the source port for the communication to the device.
  • To access a device that listens on port 501 you have to create a SnmpTarget (either CTarget or UTarget) object with a UdpAddress that has been set to the port 501.

You should easily find the relevant code in the snmpNext.cpp example.


Thank you very much @jkatz! I had missed the port on the CTarget I was using for the connection.

I ended up setting the port to 501 on both the Snmp object and the CTarget object. But it sounds like I really only need to set it in the CTarget? Am I reading that correctly?

Yes, you would only need to set the port on the Snmp object if the source port for outgoing requests of you application shall be set to a specific port.