Created
November 2, 2013 06:49
-
-
Save ddimtirov/7276317 to your computer and use it in GitHub Desktop.
A mini echo-server, I used for testing connectivity through mobile operator proxies. Feb 21, 2004
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
public class IpOperator { | |
public static void main(String[] args) throws IOException { | |
int port = 80; | |
ServerSocket srv = new ServerSocket(port); | |
try { | |
while (true) { | |
final Socket socket = srv.accept(); | |
System.out.println("== NEW Connection at port " + port + " from " + socket.getInetAddress().getCanonicalHostName() + ":" + socket.getPort()); | |
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
final PrintWriter out = new PrintWriter(socket.getOutputStream(), true); | |
final BufferedReader operator = new BufferedReader(new InputStreamReader(System.in)); | |
new Thread() { | |
public void run() { | |
try { | |
try { | |
String line = operator.readLine(); | |
if (line.equals("over")) return; | |
out.println(line); | |
} finally { | |
socket.close(); | |
} | |
} catch (IOException e) { | |
throw new Error(e.toString()); | |
} | |
} | |
}; | |
do { | |
System.out.println("IN> " + in.readLine()); | |
} while (!socket.isClosed()); | |
System.out.println("== Connection closed: " + socket.getInetAddress().getCanonicalHostName() + ":" + socket.getPort()); | |
} | |
} finally { | |
srv.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment