[Japi-cvs] SF.net SVN: japi: [54] trunk/src/app/net/sf/japi/net/Forwarder.java
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2006-04-14 15:36:47
|
Revision: 54 Author: christianhujer Date: 2006-04-14 08:36:34 -0700 (Fri, 14 Apr 2006) ViewCVS: http://svn.sourceforge.net/japi/?rev=54&view=rev Log Message: ----------- Added Socket Forwarder. Primitive, needs refactoring. Added Paths: ----------- trunk/src/app/net/sf/japi/net/Forwarder.java Added: trunk/src/app/net/sf/japi/net/Forwarder.java =================================================================== --- trunk/src/app/net/sf/japi/net/Forwarder.java (rev 0) +++ trunk/src/app/net/sf/japi/net/Forwarder.java 2006-04-14 15:36:34 UTC (rev 54) @@ -0,0 +1,83 @@ +/* + * JAPI - (Yet another (hopefully) useful) Java API + * + * Copyright (C) 2006 Christian Hujer + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +package net.sf.japi.net; + +import java.net.ServerSocket; +import java.net.Socket; +import java.io.IOException; +import net.sf.japi.io.Copier; + +/** This class forwards incoming TCP connections to another host and port. + * @author <a href="mailto:ch...@it...">Christian Hujer</a> + */ +public class Forwarder implements Runnable { + + /** Main program. + * @param args command line arguments (currently ignored) + */ + public static void main(final String... args) throws IOException { + ServerSocket server = null; + server = new ServerSocket(Integer.parseInt(args[0])); + while (true) { + final Socket client = new Socket(args[1], Integer.parseInt(args[2])); + new Forwarder(client, server.accept()).start(); + } + } + + /** First socket. */ + private final Socket s1; + + /** Second socket. */ + private final Socket s2; + + /** Create a new Forwarder. + * @param s1 first socket + * @param s2 second socket + */ + public Forwarder(final Socket s1, final Socket s2) { + this.s1 = s1; + this.s2 = s2; + } + + /** Start the forwarder. */ + public void start() { + new Thread(this).start(); + } + + /** {@inheritDoc} */ + public void run() { + try { + final Thread c1 = new Copier(s1.getInputStream(), s2.getOutputStream()).start(); + final Thread c2 = new Copier(s2.getInputStream(), s1.getOutputStream()).start(); + c1.join(); + c2.join(); + } catch (final InterruptedException ignore) { + /* ignore */ + } catch (final IOException e) { + e.printStackTrace(); //TODO + } finally { + try { s1.close(); } catch (final Exception ignore) { /* ignore */ } + try { s2.close(); } catch (final Exception ignore) { /* ignore */ } + } + } + +} // class Forwarder Property changes on: trunk/src/app/net/sf/japi/net/Forwarder.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |