views:

265

answers:

2

i have the follwoing code of proxy server. IS if the right approach? Will this be able to handel load/trafffic if deployed comerially??

package proxyserver;


import com.sun.corba.se.spi.activation.Server;
import java.net.* ;
import java.io.* ;
import java.lang.* ;
import java.util.* ;



/**
 *
 * @author user
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    // Variable to track if an error occurred
boolean errorOccurred = false;

//Variables for the host and port parameters


    public static void main(String[] args) {
        // TODO code application logic here

        int localPort = -1;
        int remotePort = -1;
        String remoteHost = "www.youtube.com";

        System.out.print("dwdsw");


        Integer parseLocalPort = new Integer(555);
        Integer parseRemotePort = new Integer(80);
        localPort =80 ;

       remotePort = 80;

        //Create a listening socket at proxy

ServerSocket server = null;
try
{
    server = new ServerSocket(localPort);
}

catch(IOException e)
{
    System.err.println("Error: " + e.getMessage());
    System.exit(-1);
}

//Loop to listen for incoming connection,
//and accept if there is one

Socket incoming = null;
Socket outgoing = null;

while(true)
{
    try
    {
     // Create the 2 sockets to transmit incoming
     // and outgoing traffic of proxy server
     incoming = server.accept();
     outgoing = new Socket(remoteHost, remotePort);

     // Create the 2 threads for the incoming
     // and outgoing traffic of proxy server
     ProxyThread thread1 = new ProxyThread(incoming, outgoing);
     thread1.start();

     ProxyThread thread2 = new ProxyThread(outgoing, incoming);
     thread2.start();
    }
    catch (UnknownHostException e)
    {
     System.err.println("Error: Unknown Host " + remoteHost);
     System.exit(-1);
    }
    catch(IOException e)
    {
     //continue
     System.exit(-2);;
    }
}




    }

}

now proxy classs

package proxyserver;

/**
 *
 * @author user
 */
import java.net.* ;
import java.io.* ;
import java.lang.* ;
import java.util.* ;

class ProxyThread extends Thread
{
    Socket incoming, outgoing;

    ProxyThread(Socket in, Socket out)
    {
     incoming = in;
     outgoing = out;
    }

    // Overwritten run() method of thread,
    // does the data transfers
    public void run()
    {
     byte[] buffer = new byte[5000];
     int numberRead = 0;
     OutputStream toClient;
     InputStream fromClient;

     try{
      toClient = outgoing.getOutputStream();
      fromClient = incoming.getInputStream();

      while(true)
      {
       numberRead = fromClient.read(buffer, 0, 50);
       if(numberRead == -1)
       {
        incoming.close();
        outgoing.close();
       }
                String st = new String(buffer,"US-ASCII");
                System.out.println("\n\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n\nXXXXXXXXXXXXXXXX\n\n" + st);


                toClient.write(buffer, 0, numberRead);
      }
     }
     catch(IOException e)
     {
     }
     catch(ArrayIndexOutOfBoundsException e)
     {
     }
    }
}
+1  A: 

It looks about right in principle but you should take a look at an open source version like TCP Proxy for pointers on maximizing throughput, increasing resilience, etc.

Nick Holt
+1  A: 

[ OK ... enough teasing :-) ]

It looks like it should work, though:

  1. The stuff that you print to System.err in the Proxy class may be mangled. (As I said before, you cannot just assume that every web page is encoded in ASCII!!)
  2. You should probably be reading much more than 50 bytes at a time .... especially if you want high throughput.
  3. Your main class probably should be using a thread pool rather than creating and throwing away threads. And you probably should put an upper bound on the number of threads you want to allow at any given time.
  4. You probably need to do something about servers that take a long time to deliver their responses, etcetera.

Finally, in response to this:

Will this be able to handle the load/traffic if it is deployed commercially??

It is impossible to say how much load you could pump through this program. For a start, it will depend on your processor and network interface hardware.

Stephen C