uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

EchoServer.java (928B)


      1 import java.net.*;
      2 import java.io.*;
      3 
      4 public class EchoServer {
      5 	public static void main(String[] args) throws IOException {
      6 		ServerSocket srvsock;
      7 		Socket cltsock;
      8 		BufferedReader in;
      9 		PrintWriter out;
     10 		String line;
     11 		int port = 7777;
     12 
     13 		if (args.length == 0)
     14 			System.err.println("usage: java EchoServer [port]");
     15 		else
     16 			port = Integer.parseInt(args[0]);
     17 		System.out.println("[EchoServer] starting");
     18 		try {
     19 			srvsock = new ServerSocket(port);
     20 			while (true) {
     21 				cltsock = srvsock.accept();
     22 				out = new PrintWriter(
     23 				    cltsock.getOutputStream(), true);
     24 				in = new BufferedReader(new InputStreamReader(
     25 				    cltsock.getInputStream()));
     26 				while ((line = in.readLine()) != null) {
     27 					if (line.equals("exit"))
     28 						break;
     29 					out.println(line);
     30 				}
     31 			}
     32 		} catch (IOException e) {
     33 			System.err.println("[EchoServer] exception on port " + port);
     34 			System.err.println(e.getMessage());
     35 		}
     36 	}
     37 }