tomcat - Binding Low Port to Java Program on Ubuntu Server -
i have mini listener/daemon catch snmp trap. listener executed while running main project (spring web) on tomcat7. got error java.net.bindexception: permission denied
i have try use authbind (http://java-notes.com/), problem not solved. had tried change port greater one, got error java.net.bindexception: cannot assign requested address
this application/trap-receiver code:
@component public class trapreceiver extends thread implements commandresponder { @inject private applicationcontext applicationcontext; @inject private executor executor; public trapreceiver(){ } list<pduv1> listpdu = new arraylist<pduv1>(); 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<pduv1> temp = new arraylist<pduv1>(); 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 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<networkinterface> net = null; try { net = networkinterface.getnetworkinterfaces(); } catch (socketexception e) { throw new runtimeexception(e); } while(net.hasmoreelements()){ networkinterface element = net.nextelement(); enumeration<inetaddress> addresses = element.getinetaddresses(); while (addresses.hasmoreelements()){ inetaddress ip = addresses.nextelement(); if (ip instanceof inet4address){ if (ip.issitelocaladdress()){ ipaddress = ip.gethostaddress(); } } } } return ipaddress; } }
you need root
bind low port on linux systems. sure run java application sufficient privileges. when connecting high port sure not in use other process/service.
see binding privileged port on debian/ubuntu enabling authbind
on ubuntu.
Comments
Post a Comment