0

I want a proxy in java that get response of a server in socket and change the html code and then pass the client as a response in socket connection.
Now My program can get request from client and can get response that from server and pass that to client Properly.
but I can not change the html code.This is my code:

Thread thread = new Thread() {
    public void run() {
        int bytesRead;
        try {
            while ((bytesRead = streamFromClient.read(request)) != -1) {
                streamToServer.write(request, 0, bytesRead);
                streamToServer.flush();
            }
            Logging incomingLog = new Logging("Incoming", tmpClient.toString());
            incomingLog.doLog();
        } catch (IOException e) {
            Logging IOExceptionLog = new Logging("Error", "Proxy cannot read client request - Client: "
                    + tmpClient.toString() + ".\nException : " + e.getMessage() + "\n");
            try {
                IOExceptionLog.doLog();
            } catch (IOException e1) {
                //Ignore me!!!
            }
        }

        // the client closed the connection to us, so close our
        // connection to the server.
        try {
            streamToServer.close();
        } catch (IOException e) {
            Logging log = new Logging("Error", "Proxy could not close connection to server.");
            try {
                log.doLog();
            } catch (IOException e1) {
                //Ignore me!!!
            }
        }
    }
};

thread.start();// Start the client-to-server request thread running;

// Read the server's responses
// and pass them back to the client;
int bytesRead;
InputStream tempStreamFromServer = streamFromServer;

/*ConvertStream convertor = new ConvertStream();
  String htmlCode = convertor.getStringFromInputStream(tempStreamFromServer);*/

try {
    while ((bytesRead = streamFromServer.read(response)) != -1) {
        streamToClient.write(response, 0, bytesRead);
        streamToClient.flush();
    }

    Logging incomingLog = new Logging("OutComing", tmpClient.toString());
    incomingLog.doLog();
} catch (IOException e) {
    Logging IOExceptionLog = new Logging("Error", "Proxy cannot send client response - Client: "
            + tmpClient.toString() + ".\nException : " + e.getMessage() + "\n");
    IOExceptionLog.doLog();
}

// The server closed its connection to us, so we close our
// connection to our client.
streamToClient.close();

Can anyone help me?

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
arVahedi
  • 107
  • 1
  • 3
  • 15
  • Don't flush inside loops. If you're using a buffered output stream, it negates the point, and if you aren't, it's pointless. – user207421 Feb 03 '15 at 08:51

1 Answers1

0

I finally can find problem.

problem was in threads.I had two thread in my program:

1.Thread A: get response of server.

2.Thread B: change the html code and pass them to client.

Now the problem was that when thread A had a loop that took responses from servers and pass them to thread B, and thread B also had a loop that wait for responses from thread A.

Now when thread B want change html code, this action give a delay to thread B and thread A don't wait for thread B.

I solved this problem with wait thread A after pass one response to thread B and notify thread A from thread B after process html code.

for more information read : http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

arVahedi
  • 107
  • 1
  • 3
  • 15