Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

socket programin in java

 
Santhana Lakshmi.S
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you mean by "it does not work"? http://faq.javaranch.com/view?TellTheDetails
 
Santhana Lakshmi.S
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, what do you mean by "not working"?

What do you expect to happen and why?

What happens instead?
 
Santhana Lakshmi.S
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic