0

我将字符串发送到服务器,但服务器在我发送的字符串之前得到了奇怪的字符,我尝试在发送之前、发送之后和初始化输出流变量之后刷新,但结果是一样的。我听说过很多关于刷新() 并搜索它,但仍然没有找到如何解决我的问题,也许它很简单,但我无法得到它。

请帮我!

客户端

    InetAddress endereco = InetAddress.getByName(null); 
    socket= new Socket(endereco, SServer.PORTO);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(new ObjectOutputStream(socket.getOutputStream()),true);

    while(true){                        
        System.out.println("Write your Thoughs.. ");
        Scanner scanner = new Scanner(System.in);
        String Msg = scanner.nextLine();
        System.out.println("I Said: ");
        System.out.println(Msg);
        out.println(Msg);
        out.flush();
        String s  = in.readLine();
        System.out.println("Echo: "+s);

}

//服务端

in = new BufferedReader(new  InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
    out.flush();
}

private void serve() throws IOException {
                    while(true){
        String s = in.readLine();
        System.out.println("Server Received: " + s);
        if(s.equals("END"))
            break;
        System.out.println("Server Send: " + s);
        out.println(s);     


    }
4

1 回答 1

0

我认为正在发生的事情是,当您编写字符串时,您将它们推过ObjectOutputStream,它试图将它们序列化(我认为)。你让事情变得比必要的更困难。尝试使用

out = new PrintWriter(socket.getOutputStream());

那只会直接通过该行转储文本,而无需所有其他内容。

于 2013-07-29T17:53:26.743 回答