Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向telnet提供下一行输入_Java_Io_Telnet - Fatal编程技术网

Java 向telnet提供下一行输入

Java 向telnet提供下一行输入,java,io,telnet,Java,Io,Telnet,当我通过telnet访问本地主机和端口时,我能够连接,但只要我给出一个输入,它就会写回。我想要的是在按下enter键时等待下一行,然后给我所有行的输出 public class NewIOServer { public static void main(String[] args) throws IOException { ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.bind(new InetSocketA

当我通过telnet访问本地主机和端口时,我能够连接,但只要我给出一个输入,它就会写回。我想要的是在按下enter键时等待下一行,然后给我所有行的输出

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());
        }

    }
}

}
公共类NewIOServer{
公共静态void main(字符串[]args)引发IOException{
ServerSocketChannel ssc=ServerSocketChannel.open();
ssc.bind(新的InetSocketAddress(“localhost”,9000));
ExecutorService池=Executors.newFixedThreadPool(100);
while(true){
SocketChannel sc=ssc.accept();
System.out.println(“连接自:“+sc”);
试一试{
ByteBuffer缓冲区=ByteBuffer.allocateDirect(1024);
而(sc.read(缓冲区)!=-1){
flip();
对于(int i=0;i
在您的代码中,我看不到任何东西实际上是在等待一行完成后才返回。Telnet客户端传统上用于交互式shell会话,通常会尽快发送数据包以减少击键后的延迟,因此您的代码当然会立即接收每一次击键并将其写回频道。

它应该可以正常工作。你使用什么操作系统?什么telnet客户端?似乎这是与客户端相关的问题(我认为在确认输入之前客户端不应刷新)。我使用的是windows 7 64位,我通过“windows功能”安装了telnet客户端。也许您可以尝试其他telnet客户端?例如,这个循环
For(inti=0;i什么也不做。移除它。在
write()之后需要
buffer.compact()
。而且每次读取时不需要新的
ByteBuffer
。使用相同的一个,在循环之前初始化。这里没有试图实现目标的代码。如果你是一个真正的Telnet服务器,你可以让自己进入在线模式,但你不是。不清楚你在问什么。