read() is not returning -1
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.
byte clientRequest = null;
Socket extSocket = null;
byte buffer = new byte;
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)
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();
// receive response from server and send to client
InputStream serverInputStream = extSocket.getInputStream();
while ((data = serverInputStream.read()) > -1)
catch (Exception err)
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.