简体   繁体   English

更改套接字输入流中的html代码并传递给java中的客户端

[英]change html code in socket input stream and pass to client in java

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. 我想要Java中的代理,以在套接字中获取服务器的响应并更改html代码,然后将客户端作为响应在套接字连接中传递。
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: 但是我无法更改html代码。这是我的代码:

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? 谁能帮我?

I finally can find problem. 我终于可以找到问题了。

problem was in threads.I had two thread in my program: 问题出在线程中。我的程序中有两个线程:

1.Thread A: get response of server. 1.Thread A:获取服务器的响应。

2.Thread B: change the html code and pass them to client. 2.Thread B:更改html代码并将其传递给客户端。

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. 现在的问题是,当线程A具有一个从服务器获取响应并将其传递给线程B的循环时,线程B也具有一个等待线程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. 现在,当线程B要更改html代码时,此操作将延迟线程B,并且线程A不要等待线程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. 在将一个响应传递给线程B后,我用等待线程A解决了此问题,并在处理html代码后从线程B通知了线程A。

for more information read : http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html 有关更多信息,请阅读: http : //docs.oracle.com/javase/7/docs/api/java/lang/Object.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM