2012-07-16 46 views
0

我正在开发一款应用程序,它将充当服务器,这就是将会有一台平板电脑将成为“服务器”,并且会有其他平板电脑将连接到“服务器”。 我想用一个选择器来使用java NIO来保存线程。但我的问题是,我有一个在android中的线程运行的java代码,但是当线程运行时,它不会发生任何事情。在客户端,它给出和拒绝连接的例外。 代码在java应用程序中运行,但在android中不运行。Java TCP服务器NIO与Android中的选择器

我也有互联网许可。

的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(); 
     } 
} 

注:泰斯特类它只是将用作机器人之间的消息的类。

我甚至试过这code和一切顺利,但与选择器不。 我希望我自己清楚问题是什么。

感谢提前:)

+0

这里的实际问题是'连接被拒绝'。到目前为止,它与NIO或选择器完全没有关系。 – EJP 2012-07-20 02:53:33

回答

2

删除的第一个参数新的InetSocketAddress(),您使用绑定的ServerSocketChannel的一个。目前你只能绑定到127.0.0.1,这是其他主机无法看到的。通过省略参数绑定到0.0.0.0,这意味着'在所有接口上侦听'。

+0

就是这样。谢谢 ;) – 2012-07-26 15:01:32

0

确保您已要求在Android的上网权限通过进入你的AndroidManifest.xml文件,并把: 使用许可权的android:NAME =“android.permission.INTERNET对”/>

+0

噢,是的,我有这个权限;)我忘了提及;) – 2012-07-16 18:41:17

+0

如果你能够在任何简单的情况下实现连接,相关的问题将是。首先开始工作来验证网络问题可能会很好。 – 2012-07-16 18:50:04

+0

我使用了http://systembash.com/content/a-simple-java-tcp-server-and-tcp-client/的代码,它运行良好;) – 2012-07-16 19:13:18