简体   繁体   English

尝试从JTextArea回显文本

[英]Trying to echo text from JTextArea

I can't really see where the mistakes are with this. 我真的看不出这个错误在哪里。

I've set up a KeyListener for the textAreaTop component, so every time a key is pressed, a string is sent to the server, which echoes it back to the teaxtAreaBottom component. 我已经为textAreaTop组件设置了KeyListener,因此,每按一次键,就会向服务器发送一个字符串,该字符串将其回显到teaxtAreaBottom组件。 I'm aware stdIn is not being used yet. 我知道尚未使用stdIn。

When the first key is pressed, a connection is made, but when the second key is pressed I get the following error: Couldn't get I/O for the connection to localhost. 当按第一个键时,建立了连接,但是当按第二个键时,出现以下错误:无法获得与本地主机的连接的I / O。 Also, nothing is appended to the textAreaBottom component. 同样,没有任何内容附加到textAreaBottom组件。

//Client //客户

    @Override
    public void keyPressed(KeyEvent e) {
    String hostName = "localhost";
     int portNumber = 4000;

    try (Socket echoSocket = new Socket(hostName, portNumber);
         PrintWriter out = new PrintWriter(echoSocket.getOutputStream(),
                    true);
         BufferedReader in = new BufferedReader(new InputStreamReader(
                    echoSocket.getInputStream()));
         BufferedReader stdIn = new BufferedReader(
                    new InputStreamReader(System.in))) {

        String userInput = textAreaTop.getText();
        out.println(userInput);
        textAreaBottom.setText(null);
        textAreaBottom.setText(in.readLine());

    } catch (UnknownHostException ex) {
         System.err.println("Don't know about host " + hostName);
        System.exit(1);
    } catch (IOException ex) {
        System.err.println("Couldn't get I/O for the connection to "
                + hostName);
        System.exit(1);
    }
    }

// Server //服务器

public static void main(String[] args) throws IOException {

    int portNumber = 4000;

    try {
        ServerSocket serverSocket = new ServerSocket(portNumber);
        Socket clientSocket = serverSocket.accept();
        System.out.println("connection accepted");
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),
                true);
        BufferedReader in = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));
        {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                out.println(inputLine);
            }
        }
    } catch (IOException e) {
        System.out
                .println("Exception caught when trying to listen on port "
                        + portNumber + " or listening for a connection");
        System.out.println(e.getMessage());
    }
}

Any advice would be much appreciated. 任何建议将不胜感激。

Thanks 谢谢

The problem is because of two little flaws: 问题是由于两个小缺陷:

  1. You should implement keyReleased() instead of keyPressed() , because the text in the textAreaTop is being updated after keyPressed() being called. 您应该实现keyReleased()而不是keyPressed() ,因为在调用keyPressed()之后,将更新textAreaTop的文本。 So when the keyReleased() method is being called your textAreaTop has the latest text you have input. 因此,当调用keyReleased()方法时,您的textAreaTop具有您输入的最新文本。

  2. You shouldn't create that Socket object everytime you press a key. 您不应该在每次按键时都创建该Socket对象。 You should define that Socket and the references to the input/output Streams outside of the keyReleased (former keyPressed) anonymous class and method. 您应该在keyReleased (以前的keyPressed)匿名类和方法之外定义Socket和对输入/输出Streams的引用。 So every time you press a key the only job to do is to get the text from textAreaTop and out.println(text) to the server. 因此,每当您按下一个键时,唯一要做的就是将文本从textAreaTopout.println(text)到服务器。 In this case I have tested the code and it's working properly. 在这种情况下,我已经测试了代码,并且可以正常工作。

Good Luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM