对等方和软件重置连接会导致连接中止

时间:2019-03-08 11:48:32

标签: java sockets http networking network-protocols

在两种情况下,我从浏览器获取请求并返回资源或html页面。以下代码可以做到这一点:

public void sendData(String someString)  {
    switch(someString.replaceAll("\\s","")) {
    case "HTTP": 
        try {
            String string = someString.replace("HTTP", "");
            Path path = Paths.get(getClass().getClassLoader().getResource("index.html").toURI());       
            byte[] fileBytes = Files.readAllBytes(path);
            String data = new String(fileBytes);
            String statusLine = "HTTP/1.1 200 OK";
            String contentTypeLine =  "Content-Type: text/html; charset=UTF-8";
            String contentlength = "Content-Length: " + fileBytes.length;
            DataOutputStream outToClient  = new DataOutputStream(this.getConnection().getOutputStream());
            outToClient.writeBytes(statusLine);
            outToClient.writeBytes(contentlength);
            outToClient.writeBytes(contentTypeLine);
            outToClient.writeBytes(this.getServerTime());
            outToClient.writeBytes(data);
            outToClient.flush();
            outToClient.close();
            } catch(IOException | URISyntaxException e) {
            System.out.println("Exception is " + e);
        }
         break;
     default:
         try {
            String string = someString.replace("HTTP", "").replaceAll("\\s", "");
            DataOutputStream outToClient  = new DataOutputStream(this.getConnection().getOutputStream());
            String statusLine = "HTTP/1.1 200 OK";
            String contentTypeLine =  "Content-Type: jpg";
            String contentlength = "Content-Length: ";
            outToClient.writeBytes(statusLine);
            outToClient.writeBytes(contentlength);
            outToClient.writeBytes(contentTypeLine);
            outToClient.writeBytes(this.getServerTime());
            BufferedImage image = ImageIO.read(new File(this.getClass().getClassLoader().getResource(string).getFile()));
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", byteArrayOutputStream);
            byte[] size = ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
            outToClient.write(size);
            outToClient.write(byteArrayOutputStream.toByteArray());
            outToClient.flush();
            outToClient.close();
            } catch(IOException e) {
                System.out.println("Exception is " + e);
            }
             break;
    }

}

从第一种情况(HTTP)开始有效,但是当浏览器请求丢失的图像时,然后当我尝试发送图像时,出现两个错误。

Exception is java.net.SocketException: Software caused connection abort: socket write error Exception is java.net.SocketException: Connection reset by peer: socket write error

我尝试了许多其他方法将数据发送到客户端,但是它不起作用。

这是代码的主要循环: `

public static void main(String [] args) throws IOException {
    ServerSocket serverSocket = new ServerSocket(80);
    ExecutorService executor = Executors.newFixedThreadPool(100);
    CommandExecutor command = new CommandExecutor();
    Socket connection; 
    while(true) {
        connection = serverSocket.accept();
        connection.setKeepAlive(true);
        if(connection != null) {
           executor.submit(new Server(connection,command));
        }
    }
}

`

还有另一个问题可能是可以解决这个问题。当我发送html页面时,首先尝试浏览器无法识别它,而只是显示html文本而不是格式化页面,但是当我刷新时,浏览器会显示格式化页面。

Here我得到了一些解释,但是我不知道它是否有帮助,因为我保持了连接的活力。

0 个答案:

没有答案