-
May 31st, 2004, 04:33 PM
#1
Proxy/gateway alike thing in Java
Some formatting gets lost while pasting this here, original: http://www.elhalf.com/Project.java
Could be used maybe as a base for a msn alike thing. You'd have to use multithreading then though.
Code:
/*Waits for a client to connect on port 23, then if the client
enters the string 'findhost' a new socket starts listening on
port 24, when second host is connected incoming traffic is being
forwarded to the other host (hence it acts as a gateway between
those 2 hosts).
*/
import java.net.*;
import java.io.*;
public class Project extends Thread {
ServerSocket server;
int PORTNUM = 23;
public static BufferedReader reader;
public static PrintWriter writer;
public Project() {
super("Project");
try {
server = new ServerSocket(PORTNUM);
System.out.println("Project is running on port " + PORTNUM);
} catch (IOException e) {
System.err.println("Exception: " + e.toString());
System.exit(1);
}
}
public static void main(String[] args) {
Project project = new Project();
project.start();
}
public void run() {
Socket client = null;
while(true) {
if (server == null)
return;
try {
client = server.accept();
} catch (IOException e) {
System.err.println("Exception" + e.toString());
System.exit(1);
}
try {
InputStreamReader inputstream = new InputStreamReader(client.getInputStream());
reader = new BufferedReader(inputstream);
writer = new PrintWriter(new BufferedOutputStream(client.getOutputStream()), false);
writer.println("You are now connected to the Project server...");
writer.flush();
while (true) {
String inLine = reader.readLine();
System.out.println("Client1: " + inLine);
if (inLine.equalsIgnoreCase("findhost")) {
WaitForSecond second = new WaitForSecond();
second.start();
while (true) {
String publicLine = reader.readLine();
System.out.println("Client1: " + publicLine);
if (publicLine.equalsIgnoreCase("quit")) {
writer.close();
reader.close();
client.close();
break;
}
second.writer.println("Client: " + publicLine);
second.writer.flush();
}
}
}
} catch (Exception e) {
System.err.println("Exception: " + e.toString());
e.printStackTrace();
}
}
}
}
class WaitForSecond extends Thread {
ServerSocket server;
int PORTNUM = 24;
public static BufferedReader reader;
public static PrintWriter writer;
public WaitForSecond() {
super("WaitForSecond");
try {
server = new ServerSocket(PORTNUM);
System.out.println("Waiting for second host on port " + PORTNUM);
Project.writer.println("Server is waiting for second host to connect...");
} catch (IOException e) {
System.err.println("Exception: " + e.toString());
System.exit(1);
}
}
public void run() {
Socket client = null;
while(true) {
if (server == null)
return;
try {
client = server.accept();
} catch (IOException e) {
System.err.println("Exception: " + e.toString());
System.exit(1);
}
try {
InputStreamReader inputstream = new InputStreamReader(client.getInputStream());
reader = new BufferedReader(inputstream);
writer = new PrintWriter(new BufferedOutputStream(client.getOutputStream()), false);
writer.println("You are connected to the Project server...");
writer.flush();
writer.println("You are the second host, you are now connected to host1");
writer.flush();
Project.writer.println("You are now connected to host2");
Project.writer.flush();
while(true) {
String newinLine = reader.readLine();
if (newinLine.equalsIgnoreCase("quit")) {
writer.close();
reader.close();
client.close();
break;
}
System.out.println("Client2: " + newinLine);
Project.writer.println("Client: " + newinLine);
Project.writer.flush();
}
} catch (Exception e) {
System.err.println("Exception: " + e.toString());
System.exit(1);
}
}
}
}
The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me
www.elhalf.com
-
May 31st, 2004, 05:55 PM
#2
Just a couple of notes:
1. Don't use e.toString() to print exception errors, instead, use e.getMessage() or e.getLocalizedMessage for the actual error, and e.getStackTrace() or e.printStackTrace() for the trace.
2. The whole design of this app could be rethought due to statically declared variables and so forth. You might want to consider passing the Project.writer and Project.reader objects to WaitForSecond, if only because it is restricting the capabilities to a single instance.
3. The next step could be to implement a standard known proxy protocol so that this could be used with a browser or FTP client.
Nice start though.
Chris Shepherd
The Nelson-Shepherd cutoff: The point at which you realise someone is an idiot while trying to help them.
\"Well as far as the spelling, I speak fluently both your native languages. Do you even can try spell mine ?\" -- Failed Insult
Is your whole family retarded, or did they just catch it from you?
-
May 31st, 2004, 06:28 PM
#3
Thanks for your comments, I know, while writing this thing I suddenly realised that in the way I was doing it I'd have to declare those vars statically.
3. The next step could be to implement a standard known proxy protocol so that this could be used with a browser or FTP client.
My intention was (and still is) to eventually develop a IM program that works like msn, this is esentially the same thing (sort off). Although that would require serious adaptations cause it'll require multithreading.
The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me
www.elhalf.com
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|