title: Binding Low Port to Java Program on Ubuntu Server
body: <p>I have a mini listener/daemon to catch SNMP trap. This will listener executed while running the main project (spring web) on tomcat7. But I always got error <strong>java.net.BindException: Permission denied</strong></p>
  
  <p>I have try to use authbind (<a href="http://java-notes.com/" rel="nofollow">http://java-notes.com/</a>),  but my problem not solved.
  I also had tried to change the port to greater one, but I got error java.net.BindException: Cannot assign requested address </p>
  
  <p>This is my application/trap-receiver code:</p>
  
  <pre><code>@Component
  public class TrapReceiver extends Thread implements CommandResponder {
  
      @Inject
      private ApplicationContext applicationContext;
  
      @Inject
      private Executor executor;
  
      public TrapReceiver(){
      }
  
      List&lt;PDUv1&gt; listPdu = new ArrayList&lt;PDUv1&gt;();
      String message = "";
      long totReceivedTrap = 0;
  
      @PostConstruct
      public void init() {
          System.out.println("Running trap listener");
          this.start();
      }
  
      public synchronized void processPdu(CommandResponderEvent cmdRespEvent) {
          PDUv1 pdu = (PDUv1) cmdRespEvent.getPDU();
          listPdu.add(pdu);
          if (pdu != null) {
              if(listPdu.size() == 10){ //10 trap per thread
                  List&lt;PDUv1&gt; temp = new ArrayList&lt;PDUv1&gt;();
                  temp.addAll(listPdu);
                  TrapInsertor trapInsertor = (TrapInsertor) applicationContext.getBean("trapInsertor");
                  trapInsertor.setProperty(temp);
                  executor.execute(trapInsertor);
                  listPdu.clear();
              }
          }
      }
  
      public void run() {
          while (true) {
              try {
                  this.listen(new UdpAddress(getIp()+"/162")); //alamat PDU akan listen
              } catch (Exception e) {
                  e.printStackTrace();
              } 
          }
      }
  
      public synchronized void listen(TransportIpAddress address)
              throws IOException {
          AbstractTransportMapping transport;
          if (address instanceof TcpAddress) {
              transport = new DefaultTcpTransportMapping((TcpAddress) address);
          } else {
              transport = new DefaultUdpTransportMapping((UdpAddress) address);
          }
  
          ThreadPool threadPool = ThreadPool.create("DispatcherPool", 10);
          MessageDispatcher mDispathcher = new MultiThreadedMessageDispatcher(
                  threadPool, new MessageDispatcherImpl());
  
          // add message processing models
          mDispathcher.addMessageProcessingModel(new MPv1());
          mDispathcher.addMessageProcessingModel(new MPv2c());
  
          // add all security protocols
          SecurityProtocols.getInstance().addDefaultProtocols();
          SecurityProtocols.getInstance().addPrivacyProtocol(new Priv3DES());
  
          // Create Target
          CommunityTarget target = new CommunityTarget();
          target.setCommunity(new OctetString("public"));
  
          Snmp snmp = new Snmp(mDispathcher, transport);
          snmp.addCommandResponder(this);
  
          transport.listen();
          System.out.println("Listening on " + address);
  
          try {
              this.wait();
          } catch (InterruptedException ex) {
              Thread.currentThread().interrupt();
          }
      }
  
      //fungsi untuk mendapatkan real ip local (bukan 127.0.0.1)
      public static String getIp(){
          String ipAddress = null;
          Enumeration&lt;NetworkInterface&gt; net = null;
          try {
              net = NetworkInterface.getNetworkInterfaces();
          } catch (SocketException e) {
              throw new RuntimeException(e);
          }
  
          while(net.hasMoreElements()){
              NetworkInterface element = net.nextElement();
              Enumeration&lt;InetAddress&gt; addresses = element.getInetAddresses();
              while (addresses.hasMoreElements()){
                  InetAddress ip = addresses.nextElement();
                  if (ip instanceof Inet4Address){
                      if (ip.isSiteLocalAddress()){
                          ipAddress = ip.getHostAddress();
                      }
                  }
              }
          }
          return ipAddress;
      }
  
  }
  </code></pre>