0

TL;DR

I've followed this tutorial to create a webserver in java.
However, the following part always fails and as result, the whole web server crashes or hangs up on this while loop:

BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));

StringBuilder requestBuilder = new StringBuilder();
String line;
while (!(line = br.readLine()).isEmpty()) {
    requestBuilder.append(line + "\r\n");
}

Sometimes it throws a NullPointerException at the while loop line. Sometimes it just hangs up or crashes. Sometimes it takes ages to do anything and sometimes it works. I also tried checking if the line String is null or if it == "" but this also lets the server crash.

A little more info:
This code is running on JDK 1.8 as a plugin in a Minecraft server on a separate thread (Runnable).
I don't know what I should do anymore. I've tried so much and it just doesn't work.

I hope someone could help me.

Best regards,

Max

EDIT:

My current state of "testing the loop" is the following:

String line = br.readLine();
while(line != null) {
    if(line.isEmpty()) {
        line = null;
    } else {
        requestBuilder.append(line);
        line = br.readLine();   
    }
}

It works and loads the website. But after this, it just crashes. I've put in a logger in the guessContentType() function (you can see it in the tutorial I've referred to at the beginning of this question). It looks like the browser is requesting the index.html file and after it gets it from the server it requests all CSS and js (assets) files that were imported. At this point, the server still runs but round about 3 seconds after it loaded all assets something is requesting the index.html file again and then it crashes.

EDIT 2: Another thing to mention. I just tried making a get request via postman. It turns out when making a GET request over postman and not the browser it gives back an ECONNRESET error in postman and I can see the index.html file that killed the server as described in the first edit.

  • 3
    Have you tried replacing your condition by this `while ((line = br.readLine()) != null && !line.isEmpty())` ? – Yassin Hajaj Apr 10 '21 at 00:24
  • @YassinHajaj please look at the edit – Maximilian Braun Apr 10 '21 at 00:55
  • 1
    The 2nd version looks "OK" though it is not clear if it does what you want it to do. I think you need to write a [minimal reproducible example](ttps://stackoverflow.com/help/minimal-reproducible-example) (that runs in outside of Minecraft) so that we can understand what you are actually trying to do. – Stephen C Apr 10 '21 at 01:40
  • Having said that, it looks like you are trying to implement a web server (or a web client) in Java without using any of the Java SE HTTP support libraries or a 3rd-part HTTP library. My advice: DON'T. You don't need to reinvent the wheel. Even if some tutorial claims to show you how to do it. (In fact, that tutorial only just scratches the surface.) – Stephen C Apr 10 '21 at 01:46
  • @StephenC Could you suggest a tutorial on how to use the java SE HTTP library as i don't want to include a 3rd-party lib – Maximilian Braun Apr 10 '21 at 02:46
  • You need to explain clearly what you are trying to do first. (How can we recommend a library when we don't know what you are trying to do?) – Stephen C Apr 10 '21 at 03:18
  • @StephenC I am trying to make an HTTP server that I can open on any port. I also want to have the option to set or change the routes ("/asdf" -> asdf.html) freely. – Maximilian Braun Apr 10 '21 at 11:28
  • Put all of the relevant details into the question ..... using the [EDIT](https://stackoverflow.com/posts/67029727/edit) button. – Stephen C Apr 10 '21 at 11:32
  • Anyway, it sounds like you could use https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html .... or https://docs.oracle.com/en/java/javase/11/docs/api/jdk.httpserver/com/sun/net/httpserver/HttpServer.html – Stephen C Apr 10 '21 at 11:35
  • @StephenC Thanks, I won't update the question but rather make a new answer where I mention all of what you said. Thanks again for the help, stay save and have a nice day ^-^ – Maximilian Braun Apr 10 '21 at 11:39
  • @StephenC just a quick question. I am programming the whole thing with eclipse. But as soon as i try to use something out the com.sun.net package it throws a restriction access error because it is not a part of the official java lib. – Maximilian Braun Apr 10 '21 at 11:52
  • That would be a bad idea. An answer is supposed to answer the question that was asked >>in the question<<. If you don't update your question (as suggested), then your proposed answer won't be apropos. If you are not going to fix the question, your best course is to simply delete it. (After all, you have the information that you need. And the original question is a blind alley ... and not answerable.) – Stephen C Apr 10 '21 at 11:53
  • Are you using Java 8 or Java 11? In Java 11, HTTPServer is an official public API. Eclipse is basically wrong ... – Stephen C Apr 10 '21 at 11:55
  • @StephenC i think its java 8 – Maximilian Braun Apr 10 '21 at 11:58
  • Read this: https://stackoverflow.com/questions/860187 – Stephen C Apr 10 '21 at 12:02
  • I could also just update my JDK to java 11. But I looked into my java installation folder and I saw that I have a folder called jdk-15.0.1 (and now one with jdk-11.0.10 because i installed it) – Maximilian Braun Apr 10 '21 at 12:09

0 Answers0