简体   繁体   English

在readline套接字上惊呆了Java

[英]stunned at readline socket java

I need to make a "static" chat, when my client say "PAPO", my server need to print PAPO and send PEPO to client print. 我需要进行“静态”聊天,当我的客户说“ PAPO”时,我的服务器需要打印PAPO并将PEPO发送给客户打印。 But im having problem at my readLine() on server, simple stop at this line. 但是我在服务器上的readLine()上遇到问题,仅在此行停止。

import java.net.*;
import java.io.*;

public class Servidor {

    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(6543);
            do {
                Socket s = server.accept();
                System.out.println("Servidor escutando...");
                BufferedReader entrada = new BufferedReader(
                        new InputStreamReader(s.getInputStream()));
                PrintWriter saida = new PrintWriter(s.getOutputStream());

                System.out.println(entrada.readLine());

                saida.write("PEPO");
                System.out.flush();

                entrada.close();
                saida.close();
                s.close();

            } while (true);
        } catch (UnknownHostException ex) {
            System.out.println("Host desconhecido");
        } catch (IOException ex) {
            System.out.println("Erro na conexao: " + ex.getMessage());
        }
    }
}

Client: 客户:

import java.net.*;
import java.io.*;

public class Cliente {

    public static void main(String[] args) {
        try {
            Socket s = new Socket("localhost", 6543);
            do {
                BufferedReader entrada = new BufferedReader(
                        new InputStreamReader(s.getInputStream()));
                PrintWriter saida = new PrintWriter(s.getOutputStream());

                saida.write("PAPO");

                System.out.println(entrada.readLine());


                entrada.close();
                saida.close();
                s.close();
            } while (true);
        } catch (UnknownHostException ex) {
            System.out.println("Host desconhecido");
        } catch (IOException ex) {
            System.out.println("Erro na conexao: " + ex.getMessage());
        }
    }

}

Look at what you're writing from the client: 查看您从客户端写的内容:

saida.write("PAPO");

That doesn't have a line break, so the server doesn't know whether there's more text coming in the same line. 那没有换行符,所以服务器不知道同一行是否有更多文本。 Also, because you haven't flushed your writer, it may be that no data is actually being sent. 另外,因为您还没有刷新过编写器,所以可能实际上没有任何数据在发送。 If you just change it to: 如果只是将其更改为:

saida.write("PAPO\n");
saida.flush();

I suspect you'll find it works. 我怀疑您会发现它可行。

However, I would strongly recommend that you specify an encoding when using InputStreamReader and OutputStreamWriter rather than just using the platform default. 但是,我强烈建议您在使用InputStreamReaderOutputStreamWriter时指定一种编码,而不是仅使用平台默认值。 UTF-8 is usually a good bet if you control both ends. 如果您控制两端,那么UTF-8通常是一个不错的选择。

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

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