Thread: [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. |
From: <chr...@us...> - 2006-04-16 02:22:17
|
Revision: 73 Author: christianhujer Date: 2006-04-15 19:22:08 -0700 (Sat, 15 Apr 2006) ViewCVS: http://svn.sourceforge.net/japi/?rev=73&view=rev Log Message: ----------- Minor improvements on exception handling. Modified Paths: -------------- trunk/src/app/net/sf/japi/net/Forwarder.java Modified: trunk/src/app/net/sf/japi/net/Forwarder.java =================================================================== --- trunk/src/app/net/sf/japi/net/Forwarder.java 2006-04-16 02:18:26 UTC (rev 72) +++ trunk/src/app/net/sf/japi/net/Forwarder.java 2006-04-16 02:22:08 UTC (rev 73) @@ -35,11 +35,20 @@ * @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])); + ServerSocket serverSocket = null; + serverSocket = 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(); + final Socket server = serverSocket.accept(); + Socket client = null; + try { + client = new Socket(args[1], Integer.parseInt(args[2])); + new Forwarder(client, server).start(); + } catch (final IOException e) { + //noinspection CatchGenericClass,OverlyBroadCatchBlock + try { server.close(); } catch (final Exception ignore) { /* ignore */ } + //noinspection CatchGenericClass,OverlyBroadCatchBlock + try { client.close(); } catch (final Exception ignore) { /* ignore */ } + } } } @@ -75,7 +84,9 @@ } catch (final IOException e) { e.printStackTrace(); //TODO } finally { + //noinspection CatchGenericClass,OverlyBroadCatchBlock try { s1.close(); } catch (final Exception ignore) { /* ignore */ } + //noinspection CatchGenericClass,OverlyBroadCatchBlock try { s2.close(); } catch (final Exception ignore) { /* ignore */ } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |