Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Sockets and Internet Protocols and the fly likes socket programin in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "socket programin in java" Watch "socket programin in java" New topic
Author

socket programin in java

Santhana Lakshmi.S
Ranch Hand

Joined: Aug 16, 2005
Posts: 82
hi
I need to connect the browser to the internet via one proxy server.
My proxy server is working fine.
I am sending the request to the proxy server using dataoutputstream
out.writeByte();
and tried to get the response from the proxy server using datainputstream
in.readByte().
This two process are working fine.
After that i tried to store the response from the server in a file.
I tried to sent the response back to the server by reading from the file using
printstream ps.println().but it is not working.
Can anyone tell me how to sent back the response from the proxyserver to browser
Thanks a lot
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
What exactly do you mean by "it does not work"? http://faq.javaranch.com/view?TellTheDetails


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Santhana Lakshmi.S
Ranch Hand

Joined: Aug 16, 2005
Posts: 82
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());}

}
}
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Again, what do you mean by "not working"?

What do you expect to happen and why?

What happens instead?
Santhana Lakshmi.S
Ranch Hand

Joined: Aug 16, 2005
Posts: 82
hi
If i type www.google.co.in in the browser,my application will take the browser request and send that request to the proxy server and in turn it will return the requested page datas as a response.
Using my application i am able to get the response from the proxy server.
The response i got is in gzip format.
I tried to send the response(data for the requested page) to the browser.
In browser the encoded content is getting displayed directly instead the requested page say,google.co.in home page.
How to display that content in the browser?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I fear I can't help you with that, but I will move this thread to our "Sockets and Internet Protocols" forum, where the experts are more likely to be able to help you...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: socket programin in java