multithreading - Web browsers send multiple request to custom Java server -
i have had make custom java thread pooled server part of bigger project .
i have run issue hurting neurons inside head since atleast 1 complete day , night .
what have done simple (followed online resource ). have while loop , loops forever . comes request , accepts , sends thread pool (java executor framework).
protected executorservice threadpool = executors.newfixedthreadpool(10); while(! isstopped()){ socket clientsocket = null; try { clientsocket = this.serversocket.accept(); } catch (ioexception e) { if(isstopped()) { system.out.println("server stopped.") ; return; } throw new runtimeexception("error accepting client connection", e); } this.threadpool.execute( new task(clientsocket, "test") ); //system.out.println("processed"); } this.threadpool.shutdown(); can't simpler right ?
also task class ,just accepts socket , , writes it's output stream
public class task implements runnable{ protected socket clientsocket = null; protected string servertext = null; public task(socket clientsocket, string servertext) { this.clientsocket = clientsocket; this.servertext = servertext; } public void run() { try { inputstream input = clientsocket.getinputstream(); outputstream output = clientsocket.getoutputstream(); long time = system.currenttimemillis(); output.write(("http/1.1 200 ok\n\nworkerrunnable: " + this.servertext + " - " + time + "").getbytes()); output.close(); input.close(); system.out.println("request processed: " + time); } catch (ioexception e) { //report exception somewhere. e.printstacktrace(); } } } here issue . chrome whenever write localhost:9004 in chrome , see (request processed : time) on console , multiple times , though hit url once .
how possible ? . happening multiple requests server initiated reason or java executor framework @ fault here perhaps executing 1 request multiple times or ? . dont believe either of reasons can true
firefox
when hit url ff , request processed correctly comes once on console each time hit server. (rarely behave chrome , on random moments , shows 2 request processed messages single url hit)
custom java client when hit url custom made java client application , server application's console window correctly shows 1 request processed tag each request .
i hit url 10 times , blazingly fast inside loop , , correctly received 10 request processed messages on server end
ie same chrome .
can guys please provide me insight on situation ? . apologize length of question .
thank .
there several requests sent out browser different purposes. once ordinary http request know it, there if-modified request, should grab header, , there request web-site icon.
printing out actual request sent browser might enlighten you. ;)
Comments
Post a Comment