Created
October 28, 2009 22:56
-
-
Save seungjin/220949 to your computer and use it in GitHub Desktop.
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.*; | |
import java.net.*; | |
class SimpleServer | |
{ | |
private static SimpleServer server; | |
ServerSocket socket; | |
Socket incoming; | |
BufferedReader readerIn; | |
PrintStream printOut; | |
public static void main(String[] args) | |
{ | |
int port = 8080; | |
try | |
{ | |
port = Integer.parseInt(args[0]); | |
} | |
catch (ArrayIndexOutOfBoundsException e) | |
{ | |
// Catch exception and keep going. | |
} | |
server = new SimpleServer(port); | |
} | |
private SimpleServer(int port) | |
{ | |
System.out.println(">> Starting SimpleServer"); | |
try | |
{ | |
socket = new ServerSocket(port); | |
incoming = socket.accept(); | |
readerIn = new BufferedReader(new | |
InputStreamReader(incoming.getInputStream())); | |
printOut = new PrintStream(incoming.getOutputStream()); | |
printOut.println("Enter EXIT to exit.\r"); | |
out("Enter EXIT to exit.\r"); | |
boolean done = false; | |
while (!done) | |
{ | |
String str = readerIn.readLine(); | |
if (str == null) | |
{ | |
done = true; | |
} | |
else | |
{ | |
out("Echo: " + str + "\r"); | |
if(str.trim().equals("EXIT")) | |
{ | |
done = true; | |
} | |
} | |
incoming.close(); | |
} | |
} | |
catch (Exception e) | |
{ | |
System.out.println(e); | |
} | |
} | |
private void out(String str) | |
{ | |
printOut.println(str); | |
System.out.println(str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment