简体   繁体   中英

datagram channel not receiving data possibly blocking?

I've got a datagramChannel client server application I'm building. The server is my desktop and the clients are android devices. I can send a message to the server, but it seems as though blocking guard is being activated (At least that's where the debugger takes me).

Anyway, the buffer is not getting the reply from the server. As far as I can tell the server is able to send and receive messages. But I'll post that code if asked.

Here is my client setup.

    public Client() throws IOException, InterruptedException {
        selector = Selector.open();
        h_addr = new InetSocketAddress("IP ADDRESS NOT LOOPBACK", 1234);
        sock_chan = DatagramChannel.open();
        sock_chan.configureBlocking(false);
        sock_chan.connect(h_addr);
        sock_chan.register(selector, SelectionKey.OP_WRITE);
        int handshake = 0;
        while(handshake < 4) {
            selector.select();
            Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
            while(keys.hasNext()) {
                SelectionKey key = keys.next();
                keys.remove();
                if(!key.isValid()) { continue; }
                if(key.isWritable()) { write(key, (handshake + 1)); }
                if(key.isReadable()) { read(key); }
            }
            handshake++;
        }
        selector.close();
    }

    private void write(SelectionKey key, int handshake) throws IOException {
        DatagramChannel w_chan = (DatagramChannel) key.channel();
        byte[] data = new byte[4096];
        data[0] = (byte)handshake;
        w_chan.write(ByteBuffer.wrap(data));
        key.interestOps(SelectionKey.OP_READ);
    }

    private void read(SelectionKey key) throws IOException {
        DatagramChannel r_chan = (DatagramChannel) key.channel();
        ByteBuffer r_buf = ByteBuffer.allocate(4096);
        r_buf.clear();
        int length;
        try { length = r_chan.read(r_buf); }
        catch (IOException e) {
            key.cancel();
            r_chan.close();
            return;
        }

        if(length < 0) {
            key.cancel();
            r_chan.close();
            return;
        }

        r_buf.flip();
        byte[] data = new byte[4096];
        r_buf.get(data, 0, length);
        System.out.println(Arrays.toString(data));
        key.interestOps(SelectionKey.OP_WRITE);
    }

I'm trying to do a three way handshake but cant seem to get past the first reply. I've tried using the channels read function as stated in some other SO answer (bc android?) but no luck. Read returns -1.

Soo, basically everything in my server was wrong lol. The biggest thing I think is that I wasn't flipping the buffer in my server's read function. After adding it to my server code I was able to send data back to my client.

A special no thanks to user207421 for not only not being helpful but being fairly rude about it (for multiple questions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM