Boas,
Estou a desenvolver uma app android que erá ser um servidor, ou seja, tenho a app a correr num tablet e depois outros tablets irão-se ligar a ele. Eu estou a utilizar o NIO do java com um selector, para poupar no uso das threads. Mas o problema é, eu tenho o selector feito e a correr numa aplicação java mesmo, mas quando o coloco no android, simplesmente não acontece nada, é como que se não estivesse a correr nada na determinada porta.
Quando tento ligar com uma app qualquer dá Connection Refused.
Já tenho a permissão da Internet.
Codigo java com o selector:
[font=Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif][size=4][left][background=rgb(255, 255, 255)]
Thread t = new Thread(new Runnable() {[/background][/left][/size][/font] 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();Codigo Cliente:
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(); } }Já testei até com este codigo: http://systembash.co...and-tcp-client/ e funcionou bem, agora com um selector não funciona .
Obrigado desde já
