gpt4 book ai didi

java - 比 BufferedReader.readLine() 更好的方法?

转载 作者:行者123 更新时间:2023-12-01 04:48:02 27 4
gpt4 key购买 nike

使用ServerSocketBufferedReader.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");
}

最佳答案

您可以使用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 ) ;
}

如果您采用此方法,则由您决定何时可以开始解析结果并发送回复。

关于java - 比 BufferedReader.readLine() 更好的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15497652/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com