Re: writing a proxy ..
- From: ebby83@xxxxxxxxx
- Date: 20 Feb 2006 11:45:07 -0800
Thanks all .. Im having different problems though . I cant get a method
synchronized ...
I am loggin (appending) entries into a text file with a sync block and
locking the whole class ie sync (this) .. unfortunately the text in the
file always gets overwritten ..
Its in the writedata method
//method
void writedata(String str) {
synchronized (this) {
try {
BufferedWriter w3 = new BufferedWriter(new FileWriter(file));
w3.append(str);
w3.flush();
w3.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//actual class code ...
package com.webproxy.fthread;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;
public class fthread extends Thread {
/**
* A Socket that connects to the clients browser.
*/
Socket orig;
/**
* A socket that connects to the http server.
*/
Socket dest;
/**
* The log file that logs the status of all requests.
*/
File file = new File("c:\\proxy.log");
/**
* The constructor for this thread.
*
* @param s
* The socket representing the connection to the clients
browser.
*/
public fthread(Socket s) {
this.orig = s;
}
void writedata(String str) {
synchronized (this) {
try {
BufferedWriter w3 = new BufferedWriter(new FileWriter(file));
w3.append(str);
w3.flush();
w3.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
String line;
try {
BufferedReader r = new BufferedReader(new InputStreamReader(orig
.getInputStream()));
OutputStream w = orig.getOutputStream();
InputStream r2 = null;
OutputStream w2 = null;
while (true) {
line = r.readLine(); // read line from the browser
// System.out.println(line);
if (line.equals(null) || line.equals("")) // check if this is
// the end of the
// stream.
break;
if (line.indexOf("GET") > -1 || line.indexOf("HEAD") > -1
|| line.indexOf("POST") > -1) {// CHECK the first line
// of the request for a
// HEAD ,GET ,POST to
// retrieve the
// sestination address
// for this request.
String url = line.split(" ")[1].split("/")[2]; // retrieve
// the dest
// HostName
// for this
// request.
System.out.println("\t\t" + url);
dest = new Socket(url, 80);// connect to the destination.
w2 = dest.getOutputStream(); // get input and output
// streams to the dest
// 'server'.
r2 = dest.getInputStream();
}
w2.write(line.getBytes()); // write line to server
w2.write("\015\012".getBytes()); // end it with CRLF
w2.flush(); // flush
}
w2.write("\n\n".getBytes()); // end stream with 2 blank lines
w2.flush();
System.out.println("\nNow Reading...\n\n");
int bytes;
int totbytes = 0;
byte[] bytebuf = new byte[4096];
while ((bytes = r2.read(bytebuf, 0, 4096)) > -1) {
totbytes += bytes;
// System.out.println(new String(bytebuf));
w.write(bytebuf, 0, bytes);
w.flush();
// file.write(bytebuf, 0, bytes);
}
w.close();
r2.close();
r.close();
w2.close();
String writestr = new Date().toString() + " "
+ this.orig.getInetAddress().getHostAddress() + " "
+ dest.getInetAddress().getHostName() + " "
+ String.valueOf(totbytes) + "\n";
System.out.println(writestr);
writedata(writestr);
dest.close();
} catch (IOException e) { // Catch any IO errors.
e.printStackTrace();
}
}
}
.
- References:
- writing a proxy ..
- From: ebby83
- Re: writing a proxy ..
- From: tom fredriksen
- Re: writing a proxy ..
- From: ebrahimbandookwala
- Re: writing a proxy ..
- From: tom fredriksen
- writing a proxy ..
- Prev by Date: Re: Balanced Tree
- Next by Date: Re: Balanced Tree
- Previous by thread: Re: writing a proxy ..
- Next by thread: Java Applet 1.5 plugin support for IE
- Index(es):
Relevant Pages
|