Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Multithreaded Web Server - Not recieving multiple GET requests
    text
    copied!<p>I have the starts of a very basic multi-hreaded web server, it can recieve all GET requests as long as they come one at a time. </p> <p>However, when multiple GET requests come in at the same time, sometimes they all are recieved, and other times, some are missing. </p> <p>I tested this by creating a html page with multiple image tags pointing to my webserver and opening the page in firefox. I always use shift+refresh. </p> <p>Here is my code, I must be doing something fundamentally wrong.</p> <pre><code>public final class WebServer { public static void main(String argv[]) throws Exception { int port = 6789; ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port); } catch(IOException e) { System.err.println("Could not listen on port: " + port); System.exit(1); } while(true) { try { Socket clientSocket = serverSocket.accept(); new Thread(new ServerThread(clientSocket)).start(); } catch(IOException e) { } } } } public class ServerThread implements Runnable { static Socket clientSocket = null; public ServerThread(Socket clientSocket) { this.clientSocket = clientSocket; } public void run() { String headerline = null; DataOutputStream out = null; BufferedReader in = null; int i; try { out = new DataOutputStream(clientSocket.getOutputStream()); in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); while((headerline = in.readLine()).length() != 0) { System.out.println(headerline); } } catch(Exception e) { } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload