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