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);
}
}
}
}