title: java server to handle multiple tcp connections
body: <p>im trying to write a simple web server in java. </p>
  
  <p>right now ive only got a simple program but id like to extend it so that it can serve multiple browsers by establishing multiple tcp connections.</p>
  
  <p>ive been reading about threading. my understanding is that you can make a new thread and that will continue as if its another program entirely. so with a new thread, it can be like there are 2 web servers which can serve 2 browsers, or x web servers which can serve x web browsers.</p>
  
  <p>im a bit lost on how to create new threads in java though and to give each new thread a connection.</p>
  
  <p>my thoughts are that i would have a loop like this which gets new connections and passes each new connection to a new thread</p>
  
  <pre><code>// make new ServerSocket
  while (true) {
       Socket newConn = serverSocket.accept();
       // make new thread, and pass in newConn
  }
  </code></pre>
  
  <p>can someone give me some guidance on how to move forward?
  (also if ive made an error somewhere, please do point it out. im new to threaded programming so its entirely possible ive not properly understood it)</p>
  
  <p>rob</p>
  
  <p>edit:</p>
  
  <p>k thanks all.</p>
  
  <p>i went and wrote something, that java tutorial helped a lot.</p>
  
  <p>ive got a new issue now</p>
  
  <p>i added a loop in my run() method in the new thread which contains a 10 second countdown (using Thread.sleep(1000)) whenever the server receives a request for an image, so i can see which threads are running when. (index.html has 4 images in it)</p>
  
  <p>so i requested the index.html page and my server works fine. then i opened up about a dozen new tabs. my expectation was that the request for the index.html page would be instant but it would take 10 seconds for the images to be sent to the browser (because of that delay i put in there), at which point the server would receive the request for the next index.html page, and so on. overall, i thought that the dozen index.html pages would be served instantly while it would take 10 seconds for the 4 * 12 = 36 images to be served on all tabs.</p>
  
  <p>what actually happened was it took 10 seconds to get the first 4 images, then 10 seconds for the next 4 images, etc. so rather than serving multiple web pages, my server just queues up requests and deals with one page at a time.</p>
  
  <p>i think my program is at fault. but i feel like i might not properly understand how a browser interacts with a server. i thought the browser requests new objects as the html page is parsed. so my server should be receiving dozens of requests if i open a dozen pages. i tried opening up several tabs in FF and then several windows in FF but this did not help. </p>
  
  <p>HOWEVER, when i opened up IE, FF and Chrome, and i asked for index.html at different times (about 2 seconds apart), it looked like each browser was receiving the page simultaneously, in other words, at one point, there were 12 different images being served, 4 to each browser</p>
  
  <p>so i guess im looking for a bit of confirmation that this is the expected behavior? and if so, why is it that i could only see this behavior when i opened up 3 different browsers and not when i opened up multiple tabs?</p>
  
  <p>(for those that asked, i plan ok taking a networks course next year, but im trying to do some of the basic stuff now. so half self learning, half h/w)</p>