Java Q&A
by Mike Jennings

Example 1: 

redirect1.listenport=2433
redirect1.address=sqlserver.???.com
redirect1.port=1433

redirect2.listenport=1521
redirect2.address=oracleserver.???.com
redirect3.port=1521

Listing One
import java.net.*;
import java.io.*;

public class SocketTie
{
InputStream isa,isb;
OutputStream osa,osb;

public SocketTie(Socket a,Socket b) throws IOException
  {
    init(a.getInputStream(),a.getOutputStream(),
      b.getInputStream(),b.getOutputStream());
  }
class A_run extends Thread
  {
    public void run()
      {
        readFromA();
      }
  }
class B_run extends Thread
  {
    public void run()
      {
        readFromB();
      }
  }
A_run arun;
B_run brun;
private void init(InputStream _isa,OutputStream _osa,
  InputStream _isb,OutputStream _osb)
  {
    isa=_isa; osa=_osa;
    isb=_isb; osb=_osb;
    arun=new A_run();
    brun=new B_run();
  }
public void start()
  {
    arun.start();
    brun.start();
  }
private void readFromA()
  {
    int abytes;
    byte[] buffer=new byte[1024];
    try
      {
        for (;;)
          {
            abytes=isa.read(buffer,0,1024);
            osb.write(buffer,0,abytes);
          }
      }
    catch(Exception ioe)
      {
        // underlying stream is closed
        try
          {
            osb.close();
          }
        catch(IOException ioe2)
          {
          }
        try
          {
            isa.close();
          }
        catch(IOException ioe3)
          {
          }
        return;
      }            
  }
private void readFromB()
  {
    int bbytes;
    byte[] buffer=new byte[1024];
    try
      {
        for (;;)
          {
            bbytes=isb.read(buffer,0,1024);
            osa.write(buffer,0,bbytes);
          }
      }
    catch(Exception ioe)
      {
        // underlying stream is closed
        try
          {
            osa.close();
          }
        catch(IOException ioe2)
          {
          }
        try
          {
            isb.close();
          }
        catch(IOException ioe3)
          {
          }

        return;
      }            
  }
}

Listing Two
private void listen() throws IOException
  {
    serversocket=new ServerSocket(listenport);
    for(;;)
      {
        Socket acceptedsocket=serversocket.accept();
        Socket realsocket;
        try
          {
            realsocket=new Socket(remoteAddress,remoteport);
          }
        catch(IOException ioe2)
          {
            try
              {
                acceptedsocket.close();
              }
            catch(IOException ioe1)
              {
              }
            continue;
          }
        SocketTie tie=new SocketTie(acceptedsocket,realsocket);
        tie.start(); // start both read and write threads
      }
  }


Listing Three
import java.util.*;
import java.net.*;
import java.io.*;
import com.slickjava.jdaemon.*;
import slickjava.net.SocketListen;

public class IPRedirectDaemon implements JDaemon
{
public IPRedirectDaemon()
  {
  }
public String getDaemonName()
  {
    return "IPRedirectDaemon2";
  }
public String getDisplayName()
  {
    return "Socket-Redirector";
  }
public String[] getDependentDaemons()
  {
    return null;
  }
public void parseCommandLine(String[] args)
  {
  }
Vector listenlist=new Vector();
private void load_socketlist() throws IOException
  {
    int i,listenport,remoteport;
    String key,value;
    InetAddress remotehost;
   
    for (i=1;i<100;i++)
      {
        key="redirect"+i+".listenport";
        value=System.getProperty(key);
        if (value==null) break;
        listenport=Integer.parseInt(value);
        key="redirect"+i+".address";
        value=System.getProperty(key);
        try
          {
            remotehost=InetAddress.getByName(value);
          }
        catch(UnknownHostException uhe)
          {
            System.out.println("can't resolve '"+value+"'");
            break;
          }  
        key="redirect"+i+".port";
        value=System.getProperty(key,""+listenport);
        remoteport=Integer.parseInt(value);
        listenlist.addElement(new SocketListen(listenport, 
                                                remotehost,remoteport));
      }
  }
public boolean tryToStart(JDaemonController c)
  {
    int checkpoint=0;
    c.daemonStarting(checkpoint++, 4000);

    try
      {
        System.out.println("Loading socket list...");
        load_socketlist();
        System.out.println("Socket list loaded.");
      }
    catch(IOException ioe)
      {
        ioe.printStackTrace();
      }
    int i,n=listenlist.size();
    if (n==0)
      {
        System.err.println("No ports to redirect!!");
        return false;
      }
    for (i=0;i<n;i++)
      {
        SocketListen sl=(SocketListen)listenlist.elementAt(i);
        sl.start();
      }
    c.daemonStarted();
System.out.println("IPRedirectDaemon started.");
System.out.flush();
    return true;
  }
Object waitobject=new Object();  
public void main()
  {
    synchronized(waitobject)
      {
        try
          {
            waitobject.wait();
          }
        catch(InterruptedException ie)
          {
            ie.printStackTrace(System.err);
          }
      }
  }
public boolean tryToStop(JDaemonController c)
  {
// first tell main it can exit
    System.out.println("IPRedirectDaemon: got signal to stop...");
    synchronized(waitobject)
      {
        waitobject.notify();
      }
// now stop all of the socket listeners
    int i,n=listenlist.size();
    c.daemonStopping(0, 700);
    for (i=0;i<n;i++)
      {
        SocketListen sl=(SocketListen)listenlist.elementAt(i);
        c.daemonStopping(1+i,400);
        sl.stopListening();
      }
    c.daemonStopped();
    return true;
  }
public boolean tryToPause(JDaemonController c)
  {
    return false;
  }
public boolean tryToContinue(JDaemonController c)
  {
    return false;
  }
public boolean tryToShutdown(JDaemonController c)
  {
    c.daemonNothingInteresting();
    return true;
  }
public boolean tryToGetStatus(JDaemonController c)
  {
    c.daemonNothingInteresting();
    return true;
  }
}





1


