Results 1 to 1 of 1
Thread: read() is not returning -1
- 11-30-2009, 10:46 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
read() is not returning -1
Hi,
I tried all day to fix a really simple code and I have no idea why it isn't working. Basically I'm writing a HTTP proxy that is just receiving the client request and sends it to the appropriate server (part of the user request aka HTTP header). The response of the the server is redirected back to the client. So in this early stage it should do nothing from the point of view of the user.
My problem is that when I try to read the client request read() doesn't return -1. So the loop never ends. I'm trying to do everything on the byte level because I don't want to temper with the data just passing it through.
Both while loops will never end. The method is part of class used as a thread for a basic client-server application.Java Code:byte[] clientRequest = null; Socket extSocket = null; try { byte[] buffer = new byte[2048]; int data = 0; this.clientInputStream = this.socket.getInputStream(); this.clientOutputStream = this.socket.getOutputStream(); // receive HTTP header from client ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(); while ((data = this.clientInputStream.read()) != -1) { byteBuffer.write(data); } clientRequest = byteBuffer.toByteArray(); HttpHeader header = this.parseHeader(new String(clientRequest)); extSocket = new Socket(header.getAddress(), 80); // send HTTP header to server OutputStream serverOuputStream = extSocket.getOutputStream(); serverOuputStream.write(clientRequest); serverOuputStream.flush(); // receive response from server and send to client InputStream serverInputStream = extSocket.getInputStream(); while ((data = serverInputStream.read()) > -1) { this.clientOutputStream.write(data); } System.out.println("done"); } catch(Exception e) { err.printStackTrace(); } finally { try { extSocket.close(); this.socket.close(); } catch (Exception err) {err.printStackTrace();} }
Does anyone have a clue what I am doing wrong? Or a good example how to implement an HTTP proxy server (without caching)? Would be glad about any help since I wasted several hours without any results.
Similar Threads
-
Keeps returning null
By ribbs2521 in forum New To JavaReplies: 7Last Post: 02-23-2009, 02:25 AM -
returning arrays
By cjohnson412 in forum New To JavaReplies: 4Last Post: 11-25-2008, 01:30 PM -
Why is my list returning nothing?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-05-2008, 03:51 PM -
java.io.IOException: Unable to read entire block; 493 bytes read before EOF; expected
By kushagra in forum New To JavaReplies: 5Last Post: 10-17-2008, 02:13 PM -
returning a value from an arraylist
By xkross in forum New To JavaReplies: 2Last Post: 04-18-2008, 05:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks