0

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

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

我的问题是:“ bufferedreader.ready()”总是返回false。如果我删除 teh if -satetmant if 工作“正常”。程序一直等到有 \n 或 \r 并打印到 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();

            }

        }
4

2 回答 2

0

因为除了调用 readLine() 之外,您在循环中无事可做,只需调用它即可。它会阻塞直到有东西到达。在这种情况下,或者实际上在任何情况下,您都不需要调用 ready() 。

于 2013-07-04T12:14:23.547 回答
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();

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

谢谢你的支持。

于 2013-07-04T12:34:28.383 回答