hi
Im trying to connect to internet through a proxy server.
I am doing the following:
1.Getting the request from the browser if user type say,
www.google.co.in 2.sending that request to the proxy server using my application.
3.Getting the response back from the proxyserver to my application.
After this i tried to send back the response to the browser in order to display the requested page,but it is not working.
The following is the code i used
import java.io.*;
import java.net.*;
import java.util.*;
public class SampleDate extends
Thread {
private ServerSocket dateServer;
Socket client = null; //browser socket
Socket skt = null; //new client socket
DataInputStream is = null;
DataOutputStream os = null;
DataInputStream nis = null;
DataOutputStream nos = null;
PrintStream pw = null;
FileOutputStream fos = null;
DataInputStream in = null;
DataOutputStream out = null;
int ch = 0;
char sendchar;
String line=null;
int ch1 = 0;
String response;
byte res;
int name=0;
byte b;
public static void main(String argv[]) throws Exception {
new SampleDate();
}
public SampleDate() throws Exception {
dateServer = new ServerSocket(9090);
System.out.println("Server listening on port 9090.");
this.start();
}
public void run() {
while(true) {
try {
System.out.println("Waiting for connections.");
client = dateServer.accept();
client.setKeepAlive(true);
Connect();
} catch(Exception e) {}
}
}
public void Connect()
{
try
{
nis = new DataInputStream(client.getInputStream());
// nos = new DataOutputStream(client.getOutputStream());
pw = new PrintStream(client.getOutputStream());
StoreReq();
}catch(Exception wer){System.out.println(wer.toString());}
}
public void StoreReq()throws Exception
{
fos = new FileOutputStream("req.txt");
while((ch = nis.read()) != -1)
{
//System.out.println((char)ch);
fos.write(ch); }//endofwhile
CreateClient();
}
public void CreateClient()throws Exception
{
skt = new Socket("127.0.0.1",8080);
InputStream sin = skt.getInputStream();
OutputStream sout = skt.getOutputStream();
in = new DataInputStream(sin);
out = new DataOutputStream(sout);
FileInputStream fin = new FileInputStream("req.txt");
int c =0;
while((c=fin.read())!= -1)
{
out.writeByte((char)c);
// System.out.print((char)c);
}
GetResponse();
//this.start();
}
public void GetResponse()
{
try
{
BufferedReader ins = new BufferedReader(new InputStreamReader(skt.getInputStream()));
String inputLine;
while ((inputLine = ins.readLine()) != null)
{
// System.out.println(inputLine);
pw.println(inputLine);
}
//in.close();
//pw.close();
}catch(Exception ert){System.out.println("hii "+ert.toString());}
}
}