在 Ubuntu 服务器上将低端口绑定到 Java 程序 [英] Binding Low Port to Java Program on Ubuntu Server

查看:52
本文介绍了在 Ubuntu 服务器上将低端口绑定到 Java 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个迷你侦听器/守护程序来捕获 SNMP 陷阱.这将在 tomcat7 上运行主项目(spring web)时执行.但我总是收到错误 java.net.BindException: Permission denied

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 java.net.BindException: Permission denied

我尝试使用 authbind (http://java-notes.com/),但我的问题没有解决.我也曾尝试将端口更改为更大的端口,但出现错误 java.net.BindException: 无法分配请求的地址

I have try to use authbind (http://java-notes.com/), 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

这是我的应用程序/陷阱接收器代码:

This is my 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 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<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;
    }

}

推荐答案

您需要成为 root 才能绑定到 Linux 系统上的低端口.请确保以足够的权限运行您的 Java 应用程序.连接到高端口时,请确保它尚未被其他进程/服务使用.

You need to be root to bind to a low port on Linux systems. Be sure to run your Java application with sufficient privileges. When connecting to a high port be sure it is not already in use by some other process/service.

另见 绑定到 Debian/Ubuntu 上的特权端口 关于在 Ubuntu 上启用 AUTHBIND.

See also Binding to a Privileged Port on Debian/Ubuntu about enabling AUTHBIND on Ubuntu.

这篇关于在 Ubuntu 服务器上将低端口绑定到 Java 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆