Java 如何使用SocketChannel和ByteBuffer读取Telnet输入流

Java 如何使用SocketChannel和ByteBuffer读取Telnet输入流,java,sockets,nio,telnet,bytebuffer,Java,Sockets,Nio,Telnet,Bytebuffer,我正在尝试使用SocketChannel和ByteBuffer读取一些telnet输入流 所以,为了做到这一点,我对线程做了一些修改,一个用来读取数据,另一个用来发送(写入)一些数据 在readResponse()中,我无法获得必须从流中读取的确切字节数,因此当我输入此方法时。我用read()读取一些数据,然后它离开,而循环和控制变量变为true,因此调用发送一些数据的另一个线程 如何读取InputStream中的所有数据 import java.io.BufferedReader; impor

我正在尝试使用SocketChannel和ByteBuffer读取一些telnet输入流

所以,为了做到这一点,我对线程做了一些修改,一个用来读取数据,另一个用来发送(写入)一些数据

在readResponse()中,我无法获得必须从流中读取的确切字节数,因此当我输入此方法时。我用read()读取一些数据,然后它离开,而循环和控制变量变为true,因此调用发送一些数据的另一个线程

如何读取InputStream中的所有数据

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MessageResponse {

    static Telnet telnet;

    public MessageResponse() throws IOException {
        telnet = new Telnet();
        new Producer().start();
        new Consumer().start();
    }

    class Producer extends Thread {

        @Override
        public void run() {
            while (true) {
                try {
                    telnet.sendMessage();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (IOException ex) {
                    Logger.getLogger(MessageResponse.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    class Consumer extends Thread {

        @Override
        public void run() {
            while (true) {
                try {
                    telnet.readReponse();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (IOException ex) {
                    Logger.getLogger(MessageResponse.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    class Telnet {
        //Socket s;
        String host = "";
        int port = 23;
        SocketAddress address = new InetSocketAddress(host, port);
        InputStream in = null;
        BufferedReader s_in = null;
        PrintWriter s_out = null;
        StringBuffer sb;
        Scanner scan;
        boolean control= false;
        SocketChannel channel;
        ByteBuffer buffer;
        Charset charset;

        public Telnet() throws IOException {
            channel = SocketChannel.open(address);
            channel.configureBlocking(false);
            buffer = ByteBuffer.allocate(2048);
            charset = Charset.forName("UTF-8");
            scan = new Scanner(System.in);
            sb = new StringBuffer();
        }

        synchronized void readReponse() throws InterruptedException, IOException {
            while (control== false) {
                channel.read(buffer);
                buffer.flip();
                System.out.println("Contets of Telnet ");
                while (buffer.hasRemaining()) {
                    System.out.print((char) buffer.get());
                    sb.append((char) buffer.get());
                }
                System.out.println(sb);
                control= true;
                notify();
            }
            while (control == true) {
                wait();
            }
        }

        synchronized void sendMessage() throws InterruptedException, IOException {
            while (control==true){  
            System.out.println("Enter command: ");
            String comando = scan.nextLine();
            channel.write(charset.encode(CharBuffer.wrap(comando)));
            control= false;
            notify();
            }
            while (control==false) {
                wait();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        MessageResponse mr = new MessageResponse();
    }
}

首先,你可能想看看;其次,你的锁定场景是,呃。。。复杂只是开始描述为什么我的锁场景是复杂的?你有什么建议吗?如果你有两个线程,根本没有理由使用非阻塞模式。客户机中的NIO无论如何没有多大意义。切换到Socket、InputStream和OutputStream.EJP,我尝试使用阻塞模式,但想不出如何通知“sendMessage”方法读取数据线程已被阻塞。你知道怎么做吗?你需要解释一下原因。我写过很多Telnet客户端,但从来都不需要。