Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Problem with Proxy Server Code in Java

Thread view: 
roassaf@gmail.com - 21 Mar 2007 22:52 GMT
Hi,

I am trying to write a very simple Proxy Server.
When I run the code below (it's only the main part of the code.
Everything compiles)
I have a problem that the read of input stream seem to hang.
I marked the problematic line.
(while ((bytesRead = istream.read(response)) >= 0))
It never prints "inside loop" and after a few checks I found out that
the read is the problem.
Can anyone see a problem in the code?

Thanks for you help

The code:

Integer port = new Integer(_props.getProperty("proxy.port"));
               ServerSocket serverSocket = new
ServerSocket(port.intValue());
               Socket socket = null;
               String fullRequest = "";
               while(true) {

                   socket = serverSocket.accept();

                   InputStream istream = socket.getInputStream();
                   OutputStream ostream = socket.getOutputStream();

                   StringBuffer headers = new StringBuffer();
                   int byteRead = 0;
                   while (!endOfHeaders(headers) && (byteRead =
istream.read()) >= 0) {
                       headers.append((char) byteRead);

                   }

                   HttpRequestHeader header = new
HttpRequestHeader(headers.toString());

                   ostream.write(header.asciiGetBytes(false));

                   int bytesRead = -1;
                   byte[] response = new byte[4096];
                   System.out.println("Before loop");
                   //Hangs on the read right below.
      The problem----->>>>>             while ((bytesRead =
istream.read(response)) >= 0) {
                         System.out.println("Inside loop");
                       ostream.write(response, 0, bytesRead);
                       ostream.flush();
                       System.out.write(response, 0, bytesRead);
                   }
                     System.out.println("After loop");
                   socket.close();
SadRed - 22 Mar 2007 00:35 GMT
On Mar 22, 6:52 am, roas...@gmail.com wrote:
> Hi,
>
[quoted text clipped - 50 lines]
>                       System.out.println("After loop");
>                     socket.close();

We see several unknown methods here.
If you want a good and quick help, post a small demo code that is
generally compilable, runnable and could reproduce your problem. See:
http://homepage1.nifty.com/algafield/sscce.html and
http://www.yoda.arachsys.com/java/newsgroups.html

We may need to see your client code as well.
roassaf@gmail.com - 22 Mar 2007 10:55 GMT
> On Mar 22, 6:52 am, roas...@gmail.com wrote:
>
[quoted text clipped - 58 lines]
>
> We may need to see your client code as well.

This is the entire code: (it can be fully compiled)

import java.util.*;
import java.io.*;
import com.ibm.wbi.protocol.http.*;
import com.ibm.wbi.util.*;
import java.net.Socket;
import java.net.ServerSocket;

public class ProxyServer {

   private WebCacheDB _cache;
   private Properties _props;
    /**
    * Constructor.
    */
    public ProxyServer() throws ProxyServerException {

           try {
               _props = new Properties();
               _props.load(new FileInputStream("proxy.properties"));
               _cache = new WebCacheDB(_props.getProperty("login"));
           }
           catch(Exception e) {
               throw new ProxyServerException(e.getMessage());
           }
    }

    /**
    * Starts the proxy, that is, listening and serving requests.
    */
    public void start() throws ProxyServerException {

           try {

               Integer port = new
Integer(_props.getProperty("proxy.port"));
               ServerSocket serverSocket = new
ServerSocket(port.intValue());
               Socket socket = null;
               String fullRequest = "";
               while(true) {

                   socket = serverSocket.accept();
                   /*BufferedReader reader = new BufferedReader
                       (new
InputStreamReader(socket.getInputStream()));
                   String lineRead = null;

                   //reads the request.
                   while ((lineRead = reader.readLine()) != null) {
                       fullRequest += lineRead;
                       } */

                   InputStream istream = socket.getInputStream();
                   OutputStream ostream = socket.getOutputStream();

                   StringBuffer headers = new StringBuffer();
                   int byteRead = 0;
                   while (!endOfHeaders(headers) && (byteRead =
istream.read()) >= 0) {
                       headers.append((char) byteRead);

                   }

                   HttpRequestHeader header = new
HttpRequestHeader(headers.toString());

                   ostream.write(header.asciiGetBytes(false));

                   int bytesRead = -1;
                   byte[] response = new byte[4096];
                   System.out.println("Before loop");

                   while ((bytesRead = istream.read(response)) >= 0)
{
                         System.out.println("Inside loop");
                       ostream.write(response, 0, bytesRead);
                       ostream.flush();
                       System.out.write(response, 0, bytesRead);
                   }
                     System.out.println("After loop");
                   socket.close();
                   //if(!serverSocket.isClosed())
                   //  serverSocket.close();
               }

           }
           catch(Exception e) {
                  throw new ProxyServerException(e.getMessage());
                 //System.out.println(e.getMessage());
           }

    }

    /**
    * Constructs the tables of the cache.
    */
    public void constructCacheTables() throws ProxyServerException{
           try {
               _cache.createTables();
           }
           catch(Exception e) {
               throw new ProxyServerException(e.getMessage());

           }
    }

    /**
    * Deletes the tables of the cache.
    */
    public void deleteCacheTables()throws ProxyServerException {
           try {
               _cache.deleteTables();
           }
           catch(Exception e) {
               throw new ProxyServerException(e.getMessage());
           }
       }

    /**
    * Clears the content of the cache (the tables are NOT deleted).
    */
    public void clearCache() {

           _cache.clearCache();
    }

    /**
    * Termination. This method closes resources like statements,
connections,
    * This method DOES NOT DELETE any table or table data.
    */
    public void terminate() {

           _cache.terminate();
    }
   public static void main(String[] args) throws Exception {

       ProxyServer server = new ProxyServer();
       server.start();
   }
   public static boolean endOfHeaders(StringBuffer headers) {
       int lastIndex = headers.length() - 1;
       if (lastIndex < 3 || headers.charAt(lastIndex) != '\n')
           return false;
       return (headers.substring(lastIndex - 3, lastIndex + 1)
               .equals("\r\n\r\n"));
   }
}
Esmond Pitt - 22 Mar 2007 01:32 GMT
> I am trying to write a very simple Proxy Server.
> When I run the code below (it's only the main part of the code.
> Everything compiles)
> I have a problem that the read of input stream seem to hang.

It's *supposed* to hang if there's no input. What behaviour are you
expecting?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.