Sign up ×

I am trying to write a simple Java program using ServerSockets that will send some HTML code to the browser. Here is my code:

       ServerSocket serverSocket = null;
       try {
             serverSocket = new ServerSocket(55555); 
           } catch (IOException e) 
             System.err.println("Could not listen on port: 55555.");

       Socket clientSocket = null; 
       try {
            clientSocket = serverSocket.accept();

            if(clientSocket != null)                

       } catch (IOException e) {
             System.err.println("Accept failed.");

     PrintWriter out = new PrintWriter(clientSocket.getOutputStream());

    out.println("HTTP/1.1 200 OK");
    out.println("Content-Type: text/html");
    out.println("<p> Hello world </p>");



I then go to localhost:55555 in my browser and nothing displays. I know the connection is working because the program outputs "Connected" as checked in the if statement. I have also tried outputting the data from the inputStream and that works. But the text I am trying to output in the browser is not displaying at all, the program finishes running and I get a "Problem loading page - the connection has been reset" in my browser, but no text.

I have searched the internet and it seems everyone else coding it this way is having their text display fine, they are having other problems. How can I fix this?

share|improve this question
what browser did you test? this code worked for me in chrome and firefox –  fmodos Apr 8 '13 at 21:33
I tested in firefox –  ALR Apr 8 '13 at 21:37
In Chrome I don't even get any results from the inputStream –  ALR Apr 8 '13 at 21:39

2 Answers 2

I tested your code in Chrome, Firefox, IE, and Opera and it works.

However, I would suggest that you use multi-threading and essentially spawn a new thread to handle each new request.

You can create a class that implements runnable and takes a clientSocket within the constructor. This will essentially make your custom webserver capable of accepting more than one request concurrently.

You will also need a while loop if you want to handle more than one total requests.

A good read demonstrating the above:


share|improve this answer
Thanks for the advice. –  ALR Apr 8 '13 at 22:11
I have tried it in Chrome and Firefox with my firewall off. It has worked once out of about 15 times and that was in chrome. I don't understand what's going wrong. –  ALR Apr 8 '13 at 22:12
Try adding a while(true) around all your code. Maybe your application is closing because more than one request is being made. – Apr 8 '13 at 22:17
  1. The line terminator in HTTP is \r\n. This means that you shouldn't use println(), you should use print() and add an explicit \r\n yourself to each line.

  2. The result of an HTTP GET is supposed to be an HTML document, not a fragment. Browsers are entitled to ignore or complain. Send this:

    <html> <head/> <body> <p> Hello world </p> </body> </html>

share|improve this answer

Your Answer


