0
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

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)
user207421
  • 305,947
  • 44
  • 307
  • 483
AAB
  • 1,594
  • 2
  • 25
  • 40
  • 1
    please post the client code as well. – Jiji Sep 25 '13 at 08:11
  • What is the output you get when you run the application and make the http request? – ahmedalkaff Sep 25 '13 at 08:12
  • @Jiji the Server should send HTTP response for a Request from browser – AAB Sep 25 '13 at 08:14
  • @JijiSasidharan: Please download the sources to Chromium yourself; SO doesn't post hundreds of megabytes of sources in a question ;-) – Aaron Digulla Sep 25 '13 at 08:14
  • 1
    Java doesn't have a "Socket Software Error". Please don't hide information from us: Give us the exact error, including the relevant part of the stack trace. – Aaron Digulla Sep 25 '13 at 08:15
  • 1
    possible duplicate of [Official reasons for "Software caused connection abort: socket write error"](http://stackoverflow.com/questions/2126607/official-reasons-for-software-caused-connection-abort-socket-write-error) – Lukas Knuth Sep 25 '13 at 08:17
  • @ Aaron Digulla This is exact stack trace – AAB Sep 25 '13 at 08:17
  • @Aaron Digulla: OP did not mention that the request is from browser. Thats why I asked for client code assuming he is using java client. – Jiji Sep 25 '13 at 08:19
  • @Lukas Knuth I have seen the question you have mentioned thats why am asking if the socket has closed? Please would appreciate if you answer have been trying to get this simple code working for a while now – AAB Sep 25 '13 at 08:20

1 Answers1

1

readLine() returns null when the peer has closed the connection. You therefore cannot write to it afterwards (except in the case of a shutdown, which doesn't happen in HTTP). If you're implementing HTTP you need to read the HTTP RFCs to find out how to do it properly. 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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • why does readLine return null? httpresonse is called once inside run method after it finishes executing why will readLine again read from the stream? I do see that I used HTTP 1.0 but that was a desperate move on my part as i have already spent quite some time trying to make this code work could you please explain me the concept how thread works I `m just learning Java I thought that br.readLine() reads from the stream and once the (end is reached) nothing left to read returns null and terminates the while loop is that wrong way of terminating readLine() – AAB Sep 25 '13 at 08:34
  • What part of 'when the peer has closed the connection' don't you understand? – user207421 Sep 25 '13 at 09:29
  • so should i use br.close() after reading operation is complete? or am i hopelessly wrong? :( – AAB Sep 25 '13 at 09:46
  • in browser localhost:5000/file.htm is this the correct way to send Http request to Server? – AAB Sep 25 '13 at 10:23
  • You are hopelessly wrong. You shouldn't be reading the request to EOS at all, for a start. You *must* stop guessing about this and look up the RFCs. Doing this correctly is non-trivial. – user207421 Sep 25 '13 at 18:01