Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

very simple http server not reading new line using keep alive

807580Jul 21 2010 — edited Jul 22 2010
Hi all,

I am trying to implement a very simple http server to server images mainly. It should have the keep alive feature so the images are served faster. The first call from the browser is ok and the the reply with the web content is right also and I can see it displayed in the browser. The problem is when the browser request for the 1st image (an html tag like <img source='img.jpg'>. I can see using wireshark that the call is being sent well from the browser and arrives to the server. There's no new socket created in the server so I assume that it is using the same socket, but then it is not being read by my server... (I can see the message 'READ AGAIN' and then nothing).
This is a sample of my code... any ideas?
Thanks in advance.
		 InputStream is;
		 DataOutputStream os;
		 BufferedReader br;
		try {
			is = socket.getInputStream();
		
	        os = new DataOutputStream(this.socket.getOutputStream());
	 
	      //Set up input stream filter
	       br = new BufferedReader(new InputStreamReader(is));
		
			String line = null;
			while (true) {
				
				line=null;
				System.out.println("READING AGAIN");
				
			    while ( (line = br.readLine()) != null ) {
			     //do something with this line of input
			    	System.out.println("LINE:"+line);
			    	if(line.equals("")) {
			    		break;
			    	}
			    }
			    System.out.println("HERE");
			    
			    connections++;
				
				Map<Integer, Long> sessionInfo = new HashMap<Integer, Long>();
				
				sessions.put(connections, sessionInfo);
				
				String mimeType = "text/html";

				
			
				Date now = new Date();
				StringBuffer sb2 = new StringBuffer();
				sb2.append("HTTP/1.1 200 OK\n\r" + "Date: "
						+ now.toString() + "\n\rServer: AjaxVNC\n\r"
						+ "Last-Modified: " + now.toString()
						+ "\n\rAccept-Ranges: bytes" //+ "\n\rConnection: close"
						+ "\n\rContent-Type: " + mimeType + "\n\n\r");
				String webPage = new String(FileStore.RequestPage);
				webPage = webPage.replaceFirst("<DYNAMICTEXT>", sb.toString());
				webPage = webPage.replaceAll("<TIMEOUT>", Long.toString(WASController.refreshTime));
				webPage = webPage.replaceFirst("<SESSION>", Long.toString(connections));
				sb2.append(webPage);
				
				os.writeBytes(sb2.toString());
				os.flush();
			    
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
Edited by: Daniel23 on Jul 22, 2010 3:17 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 19 2010
Added on Jul 21 2010
14 comments
2,928 views