套接字写错误

时间:2013-09-25 08:05:40

标签: java socketexception

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();
        }
    }
}

我在ds.writeBytes收到套接字软件错误没有其他异常发生我想Socket已关闭。我能够看到来自浏览器的HTTP请求,但仍然无法将任何排序信息打印到浏览器

BTW我之前提出过关于多线程服务器的问题,但由于之前的问题已经解决(可能已经解决)已经开始新的问题套接字错误

我得到的例外是

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)

1 个答案:

答案 0 :(得分:1)

当对等体关闭连接时,

readLine()返回null。因此,您之后无法写入(除非在关闭的情况下,在HTTP中不会发生)。如果您正在实施HTTP,则需要阅读HTTP RFC以了解如何正确执行此操作。猜测不够好。请注意,您获得的请求是HTTP 1.1,并且它包含一个Connection:keep-alive标头而没有Content-Length标头,这意味着在标头之后没有请求体读取。