FileDocCategorySizeDatePackage
Redirector.javaAPI DocExample3878Sat Sep 09 20:50:28 BST 2000None

Redirector

public class Redirector extends Object implements Runnable

Fields Summary
private int
port
private String
newSite
Constructors Summary
public Redirector(String site, int port)

    this.port = port;
    this.newSite = site;
  
Methods Summary
public static voidmain(java.lang.String[] args)


    int thePort;
    String theSite;
    
    try {
      theSite = args[0];
      // trim trailing slash
      if (theSite.endsWith("/")) {
        theSite = theSite.substring(0, theSite.length()-1);
      }
    }
    catch (Exception e) {
      System.out.println(
       "Usage: java Redirector http://www.newsite.com/ port");
      return;
    }
    
    try {
      thePort = Integer.parseInt(args[1]);
    }  
    catch (Exception e) {
      thePort = 80;
    }  
      
    Thread t = new Thread(new Redirector(theSite, thePort));
    t.start();
  
  
public voidrun()

    
    try {
      
      ServerSocket server = new ServerSocket(this.port); 
      System.out.println("Redirecting connections on port " 
        + server.getLocalPort() + " to " + newSite);
        
      while (true) {
        
        try {
          Socket s = server.accept();
          Thread t = new RedirectThread(s);
          t.start();
        }  // end try
        catch (IOException e) {   
        }
        
      } // end while
      
    } // end try
    catch (BindException e) {
      System.err.println("Could not start server. Port Occupied");
    }         
    catch (IOException e) {
      System.err.println(e);
    }