Java ServerSocket sending HTML code to browser

letwabourve

New Member
I am trying to write a simple Java program using ServerSockets that will send some HTML code to the browser. Here is my code:\[code\] ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(55555); } catch (IOException e) { System.err.println("Could not listen on port: 55555."); System.exit(1); } Socket clientSocket = null; try { clientSocket = serverSocket.accept(); if(clientSocket != null) System.out.println("Connected"); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } PrintWriter out = new PrintWriter(clientSocket.getOutputStream()); out.println("HTTP/1.1 200 OK"); out.println("Content-Type: text/html"); out.println("\r\n"); out.println("<p> Hello world </p>"); out.flush(); out.close(); clientSocket.close(); serverSocket.close();\[/code\]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?
 
Top