簡體   English   中英

更改套接字輸入流中的html代碼並傳遞給java中的客戶端

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

我想要Java中的代理,以在套接字中獲取服務器的響應並更改html代碼,然后將客戶端作為響應在套接字連接中傳遞。
現在,我的程序可以從客戶端獲取請求,並且可以從服務器獲取響應並將其正確地傳遞給客戶端。
但是我無法更改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();

誰能幫我?

我終於可以找到問題了。

問題出在線程中。我的程序中有兩個線程:

1.Thread A:獲取服務器的響應。

2.Thread B:更改html代碼並將其傳遞給客戶端。

現在的問題是,當線程A具有一個從服務器獲取響應並將其傳遞給線程B的循環時,線程B也具有一個等待線程A響應的循環。

現在,當線程B要更改html代碼時,此操作將延遲線程B,並且線程A不要等待線程B。

在將一個響應傳遞給線程B后,我用等待線程A解決了此問題,並在處理html代碼后從線程B通知了線程A。

有關更多信息,請閱讀: 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