Opening/Ouputting binary data via a socket.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Edward_Maybert
Posted On:   Saturday, April 17, 2004 09:08 AM

I am writing a small proxy server which listens on a port that I set my browser to use as a proxy. So far, it works in as much as it sucessfully gets requests from the browser through a socket, spawns a thread to handle the request which retrieves the web resource, and passes it back to the browser which displays the HTML or other text. The problem I am having is with retrieving images. All images show as broken in the rendered HTML. I am assuming the cause is that I am not properly handling binary files. The code (condensed version) I have for handling HTML ( which works ) is : ServerSocket s = new ServerSocket(4321); Socket incoming = s.accept(); PrintWriter out = new PrintWriter( inc   More>>

I am writing a small proxy server which listens on a port that I set my browser to use as a proxy. So far, it works in as much as it sucessfully gets requests from the browser through a socket, spawns a thread to handle the request which retrieves the web resource, and passes it back to the browser which displays the HTML or other text.


The problem I am having is with retrieving images. All images show as broken in the rendered HTML. I am assuming the cause is that I am not properly handling binary files. The code (condensed version) I have for handling HTML ( which works ) is :



			
ServerSocket s = new ServerSocket(4321);
Socket incoming = s.accept();
PrintWriter out = new PrintWriter( incoming.getOutputStream(), true /*autoflush*/);
URL u = new URL("http://some.host.com/index.html");
URLConnection connection = u.openConnection();
connection.connect();
BufferedReader inPage = new BufferedReader( new InputStreamReader( connection.getInputStream()));
String line;
while ( (line=inPage.readLine()) != null )
{
out.println(line);
}



This ( I guess obviously ) doesn't work for images, again I assume because the code is for a character stream when it should be a byte stream. I tried InputStreamReader and DataInputStream but without success; perhaps because I am using them incorrectly.


Can anyone guide me in the right direction, and tell me how to mod the code above to work when the URL in question points to a binary file ( images, pdf's, etc )?


Any help would be greatly appeciated.


Thnx,
~ed

   <<Less

Re: Opening/Ouputting binary data via a socket.

Posted By:   Christopher_Koenigsberg  
Posted On:   Saturday, April 17, 2004 02:16 PM

I think you do not want to use a PrintWriter, for writing binary data... I think you will find that an OutputStream will work better...

About | Sitemap | Contact