001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: TCPRelay.java,v $ 023 Revision 1.5 2004/05/05 21:36:35 markl 024 comment block updates 025 026 Revision 1.4 2003/01/19 09:37:12 markl 027 Javadoc & comment header updates. 028 029 Revision 1.3 2001/03/12 01:58:43 markl 030 Source code cleanup. 031 032 Revision 1.2 1999/01/10 03:34:00 markl 033 added GPL header & RCS tag 034 ---------------------------------------------------------------------------- 035*/ 036 037package kiwi.io; 038 039import java.net.*; 040import java.io.*; 041 042/** This class represents a TCP relay; it establishes a full duplex connection 043 * between two communications endpoints. 044 * 045 * @author Mark Lindner 046 */ 047 048public class TCPRelay 049 { 050 private Socket ins, outs; 051 private byte inbuf[], outbuf[]; 052 private DataInputStream lin, rin; 053 private DataOutputStream lout, rout; 054 private Thread t1, t2, t; 055 056 /** Construct a new <code>TCPRelay</code>. Creates a relay and starts the 057 * necessary threads to transfer data between the two endpoints. 058 * 059 * @param s The socket which represents the local endpoint. 060 * @param outHost The host of the remote endpoint. 061 * @param outPort The port number of the remote endpoint. 062 * 063 * @exception java.io.IOException If the relay could not be established. 064 */ 065 066 public TCPRelay(Socket s, String outHost, int outPort) throws IOException 067 { 068 ins = s; 069 outs = new Socket(outHost, outPort); 070 071 lin = new DataInputStream(ins.getInputStream()); 072 lout = new DataOutputStream(outs.getOutputStream()); 073 074 StreamRelay sr1 = new StreamRelay(lin, lout); 075 t1 = new Thread(sr1); 076 077 rin = new DataInputStream(outs.getInputStream()); 078 rout = new DataOutputStream(ins.getOutputStream()); 079 080 StreamRelay sr2 = new StreamRelay(rin, rout); 081 t2 = new Thread(sr2); 082 083 t1.start(); 084 t2.start(); 085 086 t = new Thread(new Runnable() 087 { 088 public void run() 089 { 090 _run(); 091 } 092 }); 093 t.start(); 094 } 095 096 /* thread body */ 097 098 private void _run() 099 { 100 for(;;) 101 { 102 try 103 { 104 t1.join(); 105 t2.join(); 106 break; 107 } 108 catch(InterruptedException ex) 109 { 110 } 111 } 112 113 try 114 { 115 ins.close(); 116 outs.close(); 117 } 118 catch(IOException ex) 119 { 120 } 121 ins = null; 122 outs = null; 123 } 124 125 /** Dispose of the relay. Closes the streams. */ 126 127 public void dispose() 128 { 129 130 try 131 { 132 ins.close(); 133 outs.close(); 134 } 135 catch(IOException ex) 136 { 137 } 138 } 139 140 } 141 142/* end of source file */