使用ServerSocket
和BufferedReader.readLine()
只有在客户端终止线路时才会读取输入,无论如何都要读取输入,并在线路终止之前发送输出?
ServerSocket SRVSOCK = new ServerSocket(35553);
while(true){
Socket SOCK = SRVSOCK.accept();
InputStreamReader IR = new InputStreamReader(SOCK.getInputStream());
BufferedReader BR = new BufferedReader(IR);
String MESSAGE = BR.readLine();
PrintStream PS = new PrintStream(SOCK.getOutputStream());
System.out.println(MESSAGE);
PS.print("Returned Successfully");
}
答案 0 :(得分:0)
您可以使用bufferedReader.read()
,它将一次返回一个字符值
StringBuffer response = "";
while ((c = bufferedReader.read()) != -1)
{
//Since c is an integer, cast it to a char. If it isn't -1, it will be in the correct range of char.
response.append( (char)c ) ;
}
如果你采用这种方法,你可以决定何时可以开始解析结果并发送回复。
答案 1 :(得分:0)