我正试图将一个字符串从客户端发送到服务器,但即使所有代码编译时没有错误,也没有抛出异常,也不会发送任何内容。这是我的服务器:
String nomeAccount = "";
try {
//PHASE 1: The server receives the email
try {
InputStream inStream = incoming.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
//if I print something here it works
nomeAccount = in.readLine();
//here nothing get printed
System.out.println("Nome: "+nomeAccount);
} catch (IOException ex) {
System.out.println("Not works");
Logger.getLogger(ServerController.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
incoming.close();
} catch (IOException ex) {
System.out.println("Not works");
Logger.getLogger(ServerController.class.getName()).log(Level.SEVERE, null, ex);
}
}
这是客户:
//PHASE 1: The client sends a string to the server
try {
InputStream inStream = s.getInputStream();
OutputStream outStream = s.getOutputStream();
PrintWriter out = new PrintWriter(outStream, true);
out.write(account+"n");
有几个问题
- PrintWriter吃掉任何异常抛出。如果他们被扔了,你就看不到他们了
- 使用
PrintWriter
而不是普通Writer
的目的是使用println
、print
或printf
- 除非打印新行,否则它不会刷新数据(这应该与write((一起使用,但println更清晰(