Skip to content

Instantly share code, notes, and snippets.

@pumpkincouture
Created April 3, 2015 15:18
Show Gist options
  • Save pumpkincouture/0a3eb8f2f20e9a857fbb to your computer and use it in GitHub Desktop.
Save pumpkincouture/0a3eb8f2f20e9a857fbb to your computer and use it in GitHub Desktop.
public class EchoServer {
public static void main(String[] args) throws IOException {
try {
ServerSocket serverSocket = new ServerSocket(5000);
while (true) {
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
FileInputStream file = new FileInputStream("./public/testfile.txt");
BufferedReader textReader = new BufferedReader(new InputStreamReader(file));
OutputStream outStream = clientSocket.getOutputStream();
String inputLine = in.readLine();
System.out.println("INFO: " + inputLine);
out.println(inputLine);
if (inputLine.contains("GET /")) {
out.flush();
out.write("HTTP/1.1 200 OK");
out.write("Content-Type: text/plain");
out.write("Date: " + new Date());
out.write("\r\n");
out.flush();
}
else {
out.flush();
System.out.println("In the else");
out.write("HTTP/1.1 404 Not Found");
out.flush();
}
System.out.println("before the close statement");
in.close();
}
} catch (Exception err) {
System.out.println(err);
err.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment