Skip to content

Instantly share code, notes, and snippets.

Created October 20, 2010 01:07
Show Gist options
  • Save anonymous/635560 to your computer and use it in GitHub Desktop.
Save anonymous/635560 to your computer and use it in GitHub Desktop.
alphatunnel
package alphatunnel;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.CharBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class client extends Thread {
private static CharBuffer buffer;
public static PrintWriter toClient;
public static InputStreamReader fromClient;
@Override
public void run() {
try {
Main.log("Waiting for a connection on port 1337...");
ServerSocket tunnel = new ServerSocket(1337);
Socket sock = tunnel.accept();
Main.log("\tConnection established or timed out.");
if (sock.isConnected()) {
Main.log("\tConnection established, continue.");
Main.log("Starting server thread...");
Thread sConn = new server(); sConn.start();
toClient = new PrintWriter(sock.getOutputStream(), true);
fromClient = new InputStreamReader(sock.getInputStream());
while (true) {
if (fromClient.ready()) {
fromClient.read(buffer);
Main.log("Client: " + buffer);
server.toServer.append(buffer);
}
}
} else
Main.log("\tConnection NOT established, halt.");
} catch (IOException ex) {
Logger.getLogger(client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
package alphatunnel;
import java.util.Date;
public class Main {
public static final void log(String msg) {
System.out.println(new Date() + "\t" + msg);
}
public static void main(String args[]) {
log("Starting client thread...");
Thread cConn = new client(); cConn.start();
}
}
package alphatunnel;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.CharBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class server extends Thread {
private static CharBuffer buffer;
public static PrintWriter toServer;
public static InputStreamReader fromServer;
@Override
public void run() {
try {
Main.log("Connecting to the real server...");
Socket skt = new Socket("68.199.65.198", 6776);
Main.log("A connection has been established.");
toServer = new PrintWriter(skt.getOutputStream(), true);
fromServer = new InputStreamReader(skt.getInputStream());
while (skt.isConnected()) {
if (fromServer.ready()) {
fromServer.read(buffer);
Main.log("Server: " + buffer);
client.toClient.append(buffer);
}
}
} catch (UnknownHostException ex) {
Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment