我正在尝试测试我的服务器以接收来自客户端的输出,适用于Linux终端,但是当我尝试传递另一个代码时,我总是收到空值。
这是我用来接收输入的方法:
public String receiveString() throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter (new OutputStreamWriter(client.getOutputStream()));
String input = null;
try{
input = in.readLine();
System.out.println("Inside:"+input);
return input;
}catch(NullPointerException e) {
out.println("4");
out.flush();
client.close();
} finally {
}
return input;
}
当我通过终端传递输出时,工作正常,但是当我通过上面的代码传递时,不工作,receiveString总是显示Null。
public static void main(String[] args ) throws IOException {
Socket client = null;
try {
client = new Socket("localhost", 12009);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader in = new BufferedReader (new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter (new OutputStreamWriter(client.getOutputStream()));
out.println("Test");
out.flush();
client.close();
}
所有连接都工作正常,只是这部分代码有问题......