有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android中带选择器的Java TCP服务器NIO

我正在安卓系统中开发一个应用程序,它将充当服务器,这就是,将有一个平板电脑作为“服务器”,还有其他平板电脑将连接到“服务器”。 我试图使用带有选择器的JavaNIO来节省线程。但我的问题是,在安卓中,java代码运行在一个线程中,但当线程运行时,它不会发生任何事情。在客户端,它给出了一个异常,连接被拒绝。 代码在java应用程序中运行,但在安卓中没有

我也有上网许可

java选择器:

Thread t = new Thread(new Runnable() {

    private Selector            selector;
    private ServerSocketChannel sChan;
    private List<SocketChannel> sockets;

    public void run() {

        try {
            selector = SelectorProvider.provider().openSelector();
            sChan = ServerSocketChannel.open();
            InetSocketAddress iaddr = new InetSocketAddress(InetAddress.getLocalHost(), 8000);

            sChan.configureBlocking(false);
            sChan.socket().bind(iaddr);

            System.out.println("Running on port:" + sChan.socket().getLocalPort());

            sChan.register(selector, SelectionKey.OP_ACCEPT);
            sockets = new LinkedList<SocketChannel>();

        }
        catch (IOException e) {
            e.printStackTrace();
        }

        Iterator<SelectionKey> it;

        try {
            while (true) {
                selector.select();

                it = selector.selectedKeys().iterator();
                while (it.hasNext()) {
                    SelectionKey key = it.next();

                    it.remove();
                    if (!key.isValid()) {
                        continue;
                    }

                    // Finish connection in case of an error
                    if (key.isConnectable()) {
                        SocketChannel ssc = (SocketChannel) key.channel();
                        if (ssc.isConnectionPending()) {
                            ssc.finishConnect();
                        }
                    }

                    if (key.isAcceptable()) {
                        ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
                        SocketChannel newClient = ssc.accept();

                        newClient.configureBlocking(false);
                        newClient.register(selector, SelectionKey.OP_READ);
                        sockets.add(newClient);

                        System.out.println("new client: " + newClient.socket().getInetAddress().getHostAddress());
                    }

                    if (key.isReadable()) {
                        SocketChannel sc = (SocketChannel) key.channel();
                        ByteBuffer data = ByteBuffer.allocate(sc.socket().getSendBufferSize());

                        System.out.println("new message: " + sc.socket().getInetAddress().getHostAddress());

                        if (sc.read(data) == -1) {
                            continue;
                        }

                        data.flip();

                        Teste m = (Teste) UdpUtil.byteToMessage(data.array());

                        System.out.println("message: " + m);
                        System.out.println("\n\n" + m.cenas);
                        sc.close();
                    }
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
});
t.start();

以及客户端应用程序:

InetSocketAddress isa = new InetSocketAddress(InetAddress.getByName("192.168.2.102"), 8000);

    SocketChannel sc = null;

    try {

        // Connect
        sc = SocketChannel.open();
        sc.connect(isa);

        // Read the time from the remote host. For simplicity we assume
        // that the time comes back to us in a single packet, so that we
        // only need to read once.

        byte[] message = UdpUtil.messageToByteMessage(new messages.Teste("hello there"));

        ByteBuffer buf = ByteBuffer.wrap(message);
        sc.write(buf);
    }
    finally {
        // Make sure we close the channel (and hence the socket)
        if (sc != null) {
            sc.close();
        }
}

注意:Teste类只是一个将用作机器人之间消息的类

我甚至试过这个code,一切都很顺利,但选择器没有。 我希望我能澄清问题所在

提前感谢:)


共 (2) 个答案

  1. # 1 楼答案

    删除new InetSocketAddress()的第一个参数,该参数用于绑定ServerSocketChannel。目前,您只绑定到127.0.0.1,这在其他主机上是看不到的。通过省略参数,您将绑定到0.0.0.0,这意味着“侦听所有接口”

  2. # 2 楼答案

    通过进入你的AndroidManifest,确保你已经在Android中申请了互联网许可。xml文件和放置: 使用权限android:name=“android.permission.INTERNET”/>