Skip to content

Instantly share code, notes, and snippets.

@VallarasuS
Last active March 12, 2023 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VallarasuS/a8cca3f73f5cedf85b9605d16bd95593 to your computer and use it in GitHub Desktop.
Save VallarasuS/a8cca3f73f5cedf85b9605d16bd95593 to your computer and use it in GitHub Desktop.
HelloWorldWeb
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class HelloWorldWeb {
public static final int PORT = 80;
public static void main(String [] args) throws IOException {
ServerSocket socket = new ServerSocket(HelloWorldWeb.PORT);
try {
while(true) {
Socket client = socket.accept();
OutputStream out = client.getOutputStream();
InputStreamReader in = new InputStreamReader(client.getInputStream());
BufferedReader reader = new BufferedReader(in);
String line = reader.readLine();
while (line != null && !line.isEmpty()) {
System.out.println(line);
line = reader.readLine();
}
String hello = "HTTP/1.1 200 OK \n\n Hello World";
out.write(hello.getBytes(StandardCharsets.UTF_8));
out.flush();
out.close();
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment