import java.util.Scanner; import java.net.*; import java.io.*; public class HttpServerDemo{ public static void main(String[] args) throws IOException{ int port = 3001; ServerSocket serverSocket = new ServerSocket(port); Sysytem.out.println("Server started on port " + port); while(true){ Socket socket = serverSocket.accept(); System.err.println("Client connected"); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String s; while((s = in.readLine() != null )){ System.err.println(s); if(s.isEmpty()){ break; } } OutputStream clientOuput = socket.getOutputStream(); clientOutput.write("HTTP/1.1 200 OK\r\n".getBytes()); clientOutput.write("\r\n".getBytes()); clientOutput.write("<b>Hello World</b>".getBytes()); clientOutput.write("\r\n\r\n".getBytes()); clientOutput.Flush(); System.out.println("Client connection ended"); in.close(); clientOuput.close(); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter