FileDocCategorySizeDatePackage
Redirector.javaAPI DocExample3896Sun Dec 12 10:53:20 GMT 2004None

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 ex) {
      System.out.println(
       "Usage: java Redirector http://www.newsite.com/ port");
      return;
    }
    
    try {
      thePort = Integer.parseInt(args[1]);
    }  
    catch (Exception ex) {
      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 ex) {   
        }
        
      } // end while
      
    } // end try
    catch (BindException ex) {
      System.err.println("Could not start server. Port Occupied");
    }         
    catch (IOException ex) {
      System.err.println(ex);
    }