简体   繁体   English

套接字写错误

[英]Socket Write Error

package WebServer;
import java.io.*;
import java.util.*;
import java.net.*;

final public class WebServer {
    ServerSocket server;
    public void start()throws Exception{
        server=new ServerSocket(5000);
        Socket client;
        while(true){
            client=server.accept();
            Thread t1=new Thread(new Handlehttp(client));
            t1.start();
        }
    }
    public static void main(String[] args) {
        try{
        new WebServer().start();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
final class Handlehttp implements Runnable{
    Socket s;
    DataOutputStream ds;
    BufferedReader br;
    public Handlehttp(Socket s) throws Exception {
        this.s=s;
        ds=new DataOutputStream(s.getOutputStream());
        br=new BufferedReader(new InputStreamReader(s.getInputStream()));
    }
    private synchronized void httprequest()throws Exception{
        String rqln,var;
        rqln=br.readLine();
        System.out.println("Port number : "+s.getPort());
        if(rqln!=null)
        System.out.println(rqln);
        while((var=br.readLine())!=null){
            System.out.println(var);
        }
    }
    private synchronized void httpreponse()throws Exception{
        String var,fileName;
        fileName="D:/file.htm";
        FileInputStream fis=new FileInputStream(fileName);
        int var1;
        var="HTTP/1.0 200 OK\r\n";
        var=var+"Connection: keep-alive\r\n";
        var=var+"Server : First\r\n";
        var=var+"Content-Type: text/html\r\n";
        var=var+"\r\n";
        ds.writeBytes(var);
        while((var1=fis.read())!=-1){
        ds.write(var1);
        }
    }

    @Override
    public void run() {
        try{
        httprequest();
        httpreponse();
        //s.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

I get a Socket Software Error at ds.writeBytes no other Exceptions occur I guess the Socket is closed.I`m able to see HTTP Request From browser but have still no luck printing any sort info to the browser 我在ds.writeBytes处收到套接字软件错误,没有其他异常发生,我想套接字已关闭。我能够看到来自浏览器的HTTP请求,但仍然没有运气向浏览器打印任何排序信息

BTW I did make a previous question regarding Multithreaded Server but since the previous problems have been solved(may be solved) have started new Question fro socket error 顺便说一句,我确实提出了有关多线程服务器的先前问题,但由于先前的问题已解决(可能已解决),因此开始出现新的套接字错误问题

The Exception I get is 我得到的例外是

Port number : 10225
GET /file.htm HTTP/1.1
Host: localhost:5000
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
DNT: 1
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6

Port number : 10226

java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:132)
    at java.io.DataOutputStream.writeBytes(DataOutputStream.java:276)
    at WebServer.Handlehttp.httpreponse(WebServer.java:57)
    at WebServer.Handlehttp.run(WebServer.java:82)
    at java.lang.Thread.run(Thread.java:722)

readLine() returns null when the peer has closed the connection. 当对等方关闭连接时,readLine()返回null。 You therefore cannot write to it afterwards (except in the case of a shutdown, which doesn't happen in HTTP). 因此,您以后不能再写它了(除非是关机,这种情况在HTTP中不会发生)。 If you're implementing HTTP you need to read the HTTP RFCs to find out how to do it properly. 如果要实现HTTP,则需要阅读HTTP RFC,以了解如何正确执行。 Guesswork is not good enough. 猜测工作还不够好。 Note that the request you got was HTTP 1.1, and that it contained a Connection: keep-alive header and no Content-Length header, meaning there was no request body to read after the headers. 请注意,您收到的请求是HTTP 1.1,并且其中包含一个Connection:keep-alive标头,没有Content-Length标头,这意味着在标头之后没有要读取的请求正文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM