Java forwader program

please see the attachment to know what I need for this program.

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

forwarder.java.txt
// java forwarder class
// waits for an inbound connection A on port INPORT
// when it is received, it launches a connection B to
// and creates threads to read-B-write-A and read-A-write-B.
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;
class forwarder {
public static String OUTHOST;
public static InetAddress OUTDEST;
public static short OUTPORT = 22;
public static short INPORT = 2345;
public static boolean DEBUG = true;
public static void main(String[] v) {
// get command-line parameters
if (v.length < 3) { System.err.println("args: inport outhost outport"); return; } INPORT = (short)(new Integer(v[0])).intValue(); OUTHOST = v[1]; OUTPORT = (short)(new Integer(v[2])).intValue(); // DNS lookup, done just once! System.err.print("Looking up address of " + OUTHOST + "..."); try { OUTDEST = InetAddress.getByName(OUTHOST); } catch (UnknownHostException uhe) { System.err.println("unknown host: " + OUTHOST); return; } System.err.println(" got it!"); // initialize LISTENER socket // wait for connection ServerSocket ss = null; ss = new ServerSocket(INPORT); // needs try-catch Socket s1; while(true) { // accept loop s1 = ss.accept(); // needs try-catch // now set up the second connection from here to ,
// represented by a second socket s2
// Then create the two Copier instances, as described in the
// project description, and start() the two threads.
// At that point, this main loop simply continues
// by going back to the ss.accept() call.
} // accept loop
} // main

/**
* The Copier class handles unidirectional copying from one socket to another.
* You will need to create two of these in the main loop above,
* one for each direction. You create the Copier object, and then
* create and start a Thread object that runs that Copier.
* If c is your Copier instance (created with Copier c = new Copier(sock1, sock2)),
* then the thread is Thread t = new Thread(c), and you start the thread
* with t.start(). Or, in one step, (new Thread(c)).start()
*/
static class Copier implements Runnable {
private Socket _from;
private Socket _to;
public Copier (Socket from, Socket to) {
_from = from;
_to = to;
}
public void run() {
InputStream fis;
OutputStream tos;
try {
fis = _from.getInputStream();
tos = _to.getOutputStream();
} catch (IOException ioe) {
System.err.println(“can’t get IO streams from sockets”);
return;
}
byte[] buf = new byte[2048];
int readsize;
while (true) {
try {
readsize = fis.read(buf);
} catch (IOException ioe) {
break;
}
if (readsize <= 0) break; try { tos.write(buf, 0, readsize); } catch (IOException ioe) { break; } } // these should be safe close() calls!! try { fis.close(); tos.close(); _from.close(); _to.close(); } catch (IOException ioe) { System.err.println("can't close sockets or streams"); return; } } } // class Copier } // class forwarder Java help.txt This program forwards a connection from one socket (host/port pair) to another. For example, when started on host foohost with the command line java forwarder 3333 outhost 44 then every time a connection is made to foohost:3333, a new connection is made to outhost:44 and two copier threads are created to copy the data between the two connections (one copier thread for each direction). The net result is that it appears to the user that a connection to foohost:3333 is actually a connection to outhost:44. No timeouts are needed, though thread creation is necessary. You are to print a line or two for each connection, indicating the original source (host,port) for the incoming connection, and the port used for the outbound forwarded connection. Note that this has a practical security implication, in that these status lines may be your only warning that someone else is using your forwarder! A suggested improvement is to check the incoming TCP source and reject connections from hosts other than the one you're using to test this. Thread creation is demonstrated in the threaded stalk server file. I'm also giving you the Copier class (as an inner class, defined in forwarder.java), that is thread-ready and which takes two sockets from and to and arranges to copy from the from socket to the to socket. To set up the copying, you first have the two sockets, s1 (the inbound socket from the accept() call) and s2 (the new second connection you create). You then create two Copier objects, one inbound = Copier(s1,s2) and one outbound = Copier(s2,s1), and then start both the threads: new Thread(inbound).start(); new Thread(outbound).start(); Here the inbound Copier object handles data from the initiating host (foohost) to outhost, and the outbound Copier object handles the reverse. Note that after these threads have been created, your main program can return to the accept() call to wait for more inbound connections. In this sense, forwarder acts like the threaded stalk server, tstalks.java. To get started, use forwarder.java. A good way to test your program, if you are doing development on your own workstation, is to start with java forwarder 3333 www.sitename.edu 80 Then fire up a web browser and point it at localhost:3333. You should get the site's web page (though notice we cannot rule out any "direct" subconnections). If you are working on a linux server, remotely, say hopper.cs.sitename.edu, then start the command above on hopper and then point your browser at hopper.cs.sitename.edu:3333. If you don't want to use command-line parameters, you can embed into your program appropriate values for INPORT, OUTHOST and OUTPORT (eg INPORT=3333; OUTHOST="www.sitename.com", OUTPORT=80). You may have to change the port number if you are working in a shared environment. Do not leave your forwarder running longer than you need to test it. Note that the ssh program has built-in forwarding like this.

Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER