JAVA TLS SOCKETS:bufferedreader.ready()总是假的

时间:2013-07-04 11:19:07

标签: java sockets ssl iostream readline

我想使用TLS编程小型“聊天”程序

我遇到SSL套接字和输入流的问题。 TLS部分工作正常。

我的问题是:“bufferedreader.ready()”始终返回false。 如果我删除teh if -satetmant如果工作“很好”。程序等待直到有\ n或\ r \ n并打印到System.out。但我不能在那段时间发送消息。

“客户端”

  InputStream inputstream = System.in;
  InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
  BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

        InputStream inputstream1 = socket.getInputStream();
        InputStreamReader inputstreamreader1 = new InputStreamReader(inputstream1);
        BufferedReader bufferedreader1 = new BufferedReader(inputstreamreader1);

        OutputStream outputstream = socket.getOutputStream();
        OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
        BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);

        OutputStream outputstream1 = System.out;
        OutputStreamWriter outputstreamwriter1 = new OutputStreamWriter(outputstream1);
        BufferedWriter bufferedwriter1 = new BufferedWriter(outputstreamwriter1);
        //Mitteilen, welchen Kommprofil zum verbindugn genutzt werden soll.
        bufferedwriter.write("bez\n");

        while (true) {

            if (bufferedreader.ready()) {
                bufferedwriter.write(bufferedreader.readLine() + '\n');
                bufferedwriter.flush();
            }
            if (bufferedreader1.ready()) {
                bufferedwriter1.write(bufferedreader1.readLine() + '\n');
                bufferedwriter1.flush();
            }


        }

“服务器”:

 InputStream inputstream = sslsocket.getInputStream();
    InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
    BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

        InputStream inputstream1 = System.in;
        InputStreamReader inputstreamreader1 = new InputStreamReader(inputstream1);
        BufferedReader bufferedreader1 = new BufferedReader(inputstreamreader1);

        OutputStream outputstream = System.out;
        OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
        BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);

        OutputStream outputstream1 = sslsocket.getOutputStream();
        OutputStreamWriter outputstreamwriter1 = new OutputStreamWriter(outputstream1);
        BufferedWriter bufferedwriter1 = new BufferedWriter(outputstreamwriter1);



        while (true ) {
            if (bufferedreader.ready()) {
                bufferedwriter.write(bufferedreader.readLine() + '\n');
                bufferedwriter.flush();

            }
            if (bufferedreader1.ready()) {
                bufferedwriter1.write(bufferedreader1.readLine() + '\n');
                bufferedwriter1.flush();

            }

        }

2 个答案:

答案 0 :(得分:0)

由于你在循环中除了调用readLine()之外别无他法,只需调用它即可。它会阻塞,直到有东西到来。在这种情况下,或者在任何情况下,你根本不需要调用ready()。

答案 1 :(得分:0)

我用这种解决方法“解决了”我的问题:

public class CopyStreamThread extends Thread {

    private BufferedWriter out;
    private BufferedReader in;

    public CopyStreamThread(BufferedWriter out, BufferedReader in) {
        this.out = out;
        this.in = in;
    }

    @Override
    public void run() {
        while (true) {
            try {
                out.write(in.readLine() + '\n');
                out.flush();
            } catch (IOException ex) {
                Logger.getLogger(CopyStreamThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } 

“客户为例/服务器”

new CopyStreamThread(bufferedwriter, bufferedreader).start();
        new CopyStreamThread(bufferedwriter1, bufferedreader1).start();

我将waitung划分为2个线程输入,这不是一个很好的解决方案,但它可以工作。

感谢您的支持。