Я сталкиваюсь с ошибкой записи в сокет. У меня есть многопоточная настройка веб-сервера. Клиент отправляет запрос GET на сервер. Сервер обрабатывает эту информацию, и при первой попытке записи в сокет я сталкиваюсь с этой ошибкой. Любые идеи, почему это произойдет?
Вот выброшенное исключение (я прокомментировал в коде сервера ниже строки, где выбрасывается исключение):
java.net.SocketException: Software caused connection abort: socket write error
at java.base/java.net.SocketOutputStream.socketWrite0(Native Method)
at java.base/java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:110)
at java.base/java.net.SocketOutputStream.write(SocketOutputStream.java:129)
at java.base/java.io.DataOutputStream.writeBytes(DataOutputStream.java:276)
at com.company.HttpRequest.processRequest(HttpRequest.java:85)
at com.company.HttpRequest.run(HttpRequest.java:24)
at java.base/java.lang.Thread.run(Thread.java:834)
Вот код сервера:
private void processRequest() throws Exception
{
// instance variable is a Java Socket object
InputStream is = this.socket.getInputStream();
OutputStream os = this.socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String requestLine = br.readLine();
// Assuming client sends a GET request only
StringTokenizer tokens = new StringTokenizer(requestLine);
tokens.nextToken();
String fileName = tokens.nextToken();
System.out.println(fileName);
// Prepend a "." so that file request is within the current directory.
fileName = "." + fileName;
FileInputStream fis = null;
boolean fileExists = true;
try {
fis = new FileInputStream(fileName);
} catch (FileNotFoundException e){
fileExists = false;
}
// Construct response message
String statusLine = null;
String contentTypeLine = null;
String entityBody = null;
if (fileExists){
statusLine = "200 OK";
contentTypeLine = "Content-type: " + contentType(fileName) + CRLF;
} else {
statusLine = "404 Not Found";
contentTypeLine = "Content-type: " + CRLF;
entityBody = "<HTML>" +
"<HEAD><TITLE>Not Found</TITLE></HEAD>" +
"<BODY>Not Found</BODY></HTML>";
}
dos.writeBytes(statusLine); // EXCEPTION ON THIS LINE
dos.writeBytes(contentTypeLine);
dos.writeBytes(CRLF);
if (fileExists){
sendBytes(fis, dos);
fis.close();
} else {
dos.writeBytes(entityBody);
}
// close streams and sockets
os.close();
br.close();
socket.close();
}
Вот код клиента:
'' '
static String httpRequest =
"GET /HelloWorld.html HTTP/1.1" + CRLF +
"Host: 192.168.86.132:6789" + CRLF +
"Connection: keep-alive" + CRLF +
"Cache-Control: max-age=0" + CRLF +
"Upgrade-Insecure-Requests: 1" + CRLF +
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36 Edg/80.0.361.69" + CRLF +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" + CRLF +
"Accept-Encoding: gzip, deflate" + CRLF +
"Accept-Language: en-US,en;q=0.9" + CRLF +
CRLF;
public static void main(String[] args) throws IOException {
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(httpRequest);
clientSocket.close();
}
' ''