-1

I have some server and client code that use nio in java. When I try connected to the server sctpChannel.connect(new InetSocketAddress("127.0.0.1",4545)); returns false. And selector.select() returns 0 . I can't figure out why it happens.

public class SctpNioServer {
public static void main(String[] args)throws  Exception{
    InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost",4545);
    SctpServerChannel serverChannel = SctpServerChannel.open();
    serverChannel.bind(inetSocketAddress);
    serverChannel.configureBlocking(false);
    Selector selector = Selector.open();
    serverChannel.register(selector,SelectionKey.OP_ACCEPT );
    while(true){
        try{
            selector.select();
            Set<SelectionKey> selectedKeys = selector.selectedKeys() ;
            Iterator<SelectionKey> iterator = selectedKeys.iterator();
            while (iterator.hasNext()){
                SelectionKey key = iterator.next();
                iterator.remove();
                if(key.isAcceptable()){
                    SctpChannel channel = serverChannel.accept();
                    channel.configureBlocking(false) ;
                    channel.register(selector,SelectionKey.OP_READ);
                }
                if (key.isReadable()){
                    SctpChannel channel = (SctpChannel) key.channel();
                    ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
                    channel.receive(byteBuffer,null,null);
                    byteBuffer.flip();
                    while (byteBuffer.hasRemaining()){
                        System.out.println((char)byteBuffer.get());
                    }
                    byteBuffer.clear();
                    channel.register(selector, SelectionKey.OP_WRITE);
                }
                if(key.isWritable()){
                    //doSomething
                }
            }
            selector.wakeup();
        }
        catch (IOException e){
            e.printStackTrace();
        }
        finally {
            serverChannel.close();
        }
    }
}}

public class SctpNioClient  {
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    try {
        SctpChannel channel = SctpChannel.open();
        channel.configureBlocking(false);
        channel.connect(new InetSocketAddress("localhost", 4545));
        Selector sel = Selector.open();
        channel.register(sel, SelectionKey.OP_CONNECT);
        while (true) {
            if (sel.isOpen()) {
                int keys = sel.select(5);
                if (keys > 0) {
                    Set<SelectionKey> selectedKeys = sel.selectedKeys();
                    for (SelectionKey sk : selectedKeys) {
                        if (!sk.isValid()) {
                            break;
                        }
                        if (sk.isConnectable()) {
                            channel.finishConnect();
                            channel.register(sel, SelectionKey.OP_WRITE, channel);
                        }
                        if (sk.isWritable()) {
                            SctpChannel ch = (SctpChannel) sk.channel();
                            System.out.println("writing->");
                            ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
                            String text = scanner.nextLine();
                            byteBuffer.clear();
                            byteBuffer.put(text.getBytes());
                            byteBuffer.flip();
                            MessageInfo messageInfo = MessageInfo.createOutgoing(null,null,0);
                            ch.send(byteBuffer,messageInfo);
                            ch.register(sel , SelectionKey.OP_READ);
                            sel.wakeup();
                        }
                        if(sk.isReadable()){
                            SctpChannel ch = (SctpChannel) sk.channel();
                            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                            ch.receive(byteBuffer,null,null);
                            byteBuffer.flip();
                            while (byteBuffer.hasRemaining()){
                                System.out.println((char)byteBuffer.get());
                            }
                            ch.register(sel,SelectionKey.OP_WRITE);
                        } } } } }
    } catch (IOException ex) { }
}}
  • How do you know it returns false when you aren't testing it? And why are you writing blocking-mode code in non-blocking mode? – user207421 Jul 10 '18 at 09:58
  • Please look at the code again – Complex Tuber Jul 10 '18 at 10:11
  • There is still nothing here that shows that `connect()` returned false. All that can be seen is a disagreement between port numbers 4545 and 5050. And why shouldn't it return false? It takes time, and you're non-blocking mode, – user207421 Jul 10 '18 at 10:15
  • but why it can't connect to server? sorry , Im new in java nio – Complex Tuber Jul 10 '18 at 10:25
  • There is no evidence here that it can't connect to the server. All there is is a lot of poor quality NIO code and a ridiculously short timeout of 5 milliseconds. There is nothing here to answer. You could start by addressing all that, and by printing exceptions instead of ignoring them. – user207421 Jul 10 '18 at 10:27

1 Answers1

1
  1. connect() returns a boolean. You claim that it is false, but your code ignores it.
  2. If it returns true, you don't need to register for OP_CONNECT: but as you are ignoring it you are also ignoring this fact.
  3. finishConnect() also returns a boolean, which you are also ignoring.
  4. All these operations can throw IOExceptions, which you are also ignoring.

Solution: don't. In all cases.

user207421
  • 305,947
  • 44
  • 307
  • 483