0

我正在尝试使用BufferedWriter从客户端或服务器到客户端向服务器发送消息。但是什么都不会发送,但是如果您向它发送消息,它会收听。我不确定我做错了什么,但我认为问题出在此处。

ActionListener buttonActive= new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                messageTextArea.append(textMessage.getText()+ "\n");
                sendText = textMessage.getText();
            }
        };
        sendButton.addActionListener(buttonActive);

    private void startSending(){

        SwingWorker <Void, Void> sendingThread = new SwingWorker <Void, Void>(){
            protected Void doInBackground() throws Exception {

                BufferedWriter writer = null;
                writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
                while(connected){
                    writer.write(sendText);
                    writer.flush();
                }
                return null;
            }
        };
        sendingThread.execute();
    }

@trashgod 和 @madProgrammer 给了我一个使用 printWriter 的解决方案,但我希望我使用BufferedWriter+和+制作更多版本BufferedReader,但是我想制作+ 。OutputStream+ InputStreamBufferedWriterPrintWriterBufferedWriterBufferedReader

4

1 回答 1

1

您的代码确实发送了数据。也许客户正在打电话readLine()?由于您没有发送行终止符,因此它将永远阻塞。

但是您的代码没有多大意义。在循环中编写相同的文本可能不是您想要的。并且没有PrintReader.

于 2013-02-06T00:26:25.113 回答