将下一行输入提供给telnet

时间:2015-09-08 17:25:27

标签: java io telnet

当我通过telnet访问localhost和port时,我可以连接但是只要我输入一个输入就会回写。我想要的是等待 nextline 当我按下回车然后给我所有线路的输出。

public class NewIOServer {

public static void main(String[] args) throws IOException {
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.bind(new InetSocketAddress("localhost", 9000));
    ExecutorService pool = Executors.newFixedThreadPool(100);
    while (true) {
        SocketChannel sc = ssc.accept();
        System.out.println("Connected from: " + sc);
        try {
            ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
            while (sc.read(buffer) != -1) {
                buffer.flip();
                for (int i = 0; i < buffer.limit(); i++)
                    buffer.put(i, (byte) buffer.get(i));
                sc.write(buffer);
                buffer.clear();
            }
        } catch (IOException ex) {
            System.out.println("Problems with the connections into the server - " + ex.getMessage());
        }

    }
}

}

1 个答案:

答案 0 :(得分:0)

我的代码中没有看到任何实际上在等待回送之前完成一行的内容。传统上用于交互式shell会话的Telnet客户端通常会在按键后尽快发送数据包以减少延迟,因此您的代码当然会立即收到每次击键并将其写回通道。