Java nio sctpChannel selector.select()返回0

时间:2018-07-08 13:18:10

标签: java nio sctp

我需要使用sctp编写客户端-服务器项目。当我用ServerSocketChannel编写代码时,它可以正常工作。但是我需要使用SctpServerChannel,当我在客户端运行代码选择器时,select()返回0。为什么我的代码可用于tcp连接,但同一代码不能用于sctp连接?请帮助我:(

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) { }
}

}

0 个答案:

没有答案