客户端程序:
/**
* author mwf12
*/
package com.mwf.server;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class TestServer {
public static void main(String[] args) throws IOException, InterruptedException {
Socket socket = new Socket("127.0.0.1",8080);
OutputStream outputStream = socket.getOutputStream();
String str = "POST / HTTP/1.1\n" +
"Host: 127.0.0.1:8080\n" +
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\n" +
"Accept-Language: zh-CN,en-US;q=0.8,zh;q=0.7,zh-TW;q=0.5,zh-HK;q=0.3,en;q=0.2\n" +
"Accept-Encoding: gzip, deflate\n" +
"Content-Type: application/x-www-form-urlencoded\n" +
"Content-Length: 45\n" +
"Connection: keep-alive\n" +
"Upgrade-Insecure-Requests: 1\n" +
"userName=%E5%93%88%E5%93%88%E5%93%88%E5%93%88";
outputStream.write(str.getBytes());
outputStream.flush();
Thread.sleep(200000000);
outputStream.close();
}
}
服务器端主程序:
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress("127.0.0.1",8080));
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
Scanner scanner = new Scanner(inputStream);
while(scanner.hasNextLine()){
String s = scanner.nextLine();
System.out.println(s);
}
}
程序运行截图

请问老师,为什么服务端在客户端没有关闭流或套接字的情况下,服务器端不能接收最后一行数据,除了使用一次全部读取的方法外,还有什么解决方案?