Re: writing a proxy ..



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();
}

}
}

.



Relevant Pages

  • Re: call is blocked in recvfrom() and no further proceedings in Win CE
    ... > In windows CE, I'm able to send a request but I'm unable to receive it. ... Create another socket & bind with server IP address. ... >> My program has to send request to service through port 5070(in this port ...
    (microsoft.public.windowsce.embedded)
  • MULTITHREADED
    ... where each client request are handle ... connected socket return by accept function. ... X Server close the connection without passing all the requested data ...
    (comp.programming.threads)
  • Re: Trying to GET google with socket....problem
    ... socket to www.google.ca, but you only tell it to deliver some ... In HTTP/1.0, you open a socket, issue a request, get a response and ... socket is closed by the server. ... When you specify HTTP/1.1, ...
    (comp.lang.ruby)
  • Re: Are sockets thread safe
    ... the client wraps its own request for a paraticular web page from the ... to the same web server on the other end of the same socket. ...
    (comp.os.linux.networking)
  • Re: Trying to GET google with socket....problem
    ... socket to www.google.ca, but you only tell it to deliver some ... In HTTP/1.0, you open a socket, issue a request, get a response and ... socket is closed by the server. ... When you specify HTTP/1.1, ...
    (comp.lang.ruby)