如何使用端口502

时间:2014-03-03 11:05:56

标签: android tcp modbus

我正在尝试开发一个需要modbus / TCP的应用程序。 Modbus / TCP的默认端口是502.当我尝试在端口502上打开连接时出现错误。我尝试了不同的端口,它们正在工作。我认为有些东西阻塞了端口502,但我无法理解它。

这是我得到的例外:01-01 00:01:44.309: I/System.out(953): S: Error java.net.BindException: bind failed: EACCES (Permission denied)

我有“互联网”许可

代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer extends Thread {

    public static final int SERVERPORT = 502;
    public boolean running = true;
    public boolean receiving = false;
    private PrintWriter mOut;

    public TCPServer() {
    }

    public void sendMessage(String message) {
        if (mOut != null && !mOut.checkError()) {
            mOut.println(message);
            mOut.flush();
        }
    }

    @Override
    public void run() {
        super.run();
        try {
            while (true) {
                while (running) {
                    ServerSocket serverSocket = new ServerSocket(SERVERPORT);
                    serverSocket.setSoTimeout(500);
                    Socket tempSckt = new Socket();
                    boolean isConnected = false;
                    while (!isConnected && running) {
                        try {
                            tempSckt = serverSocket.accept();
                            isConnected = true;
                        } catch (Exception e) {

                        }
                    }
                    if (running) {
                        System.out.println("connected");
                        final Socket client = tempSckt;
                        new Thread(new Runnable() {

                            @Override
                            public void run() {
                                receiving = true;
                                try {
                                    mOut = new PrintWriter(client
                                            .getOutputStream());
                                    BufferedReader in = new BufferedReader(
                                            new InputStreamReader(client
                                                    .getInputStream()));
                                    while (receiving) {
                                        String message = in.readLine();
                                        if (message != null) {
                                            // System.out.println(message);
                                            byte[] bytes = message.getBytes();
                                            StringBuilder binary = new StringBuilder();
                                            for (byte b : bytes) {
                                                int val = b;
                                                for (int i = 0; i < 8; i++) {
                                                    binary.append((val & 128) == 0 ? 0
                                                            : 1);
                                                    val <<= 1;
                                                }
                                                binary.append(' ');
                                            }
                                            System.out.println("'" + message
                                                    + "' to binary: " + binary);
                                        } else {
                                            receiving = false;
                                        }
                                    }

                                } catch (Exception e) {
                                    System.out.println("S: Error"
                                            + e.toString());
                                } finally {
                                    try {
                                        client.close();
                                    } catch (Exception e2) {
                                    }
                                }
                            }
                        }).start();
                        isConnected = false;
                    }

                    serverSocket.close();
                }
                Thread.sleep(500);
            }
        } catch (Exception e) {
            System.out.println("S: Error " + e.toString());
        }

    }
}

更新

我的应用程序作为系统应用程序运行。我做了必要的配置。我用ps shell命令检查了它。它现在作为系统应用程序工作。但我仍然无法绑定到端口502。

1 个答案:

答案 0 :(得分:1)

1024以下的端口受到限制 - 只有具有root权限的应用才能监听这些端口。当然,您的申请没有特权。

这是Linux / Unix上的一般规则(Android是基于Linux的。)

请参阅this answer了解此限制背后的理由。