rt

Reader's Theatre
Log | Files | Refs | README | LICENSE

client.java (1092B)


      1 package space.nocebo;
      2 
      3 import java.net.*;
      4 import java.io.*;
      5 import java.util.concurrent.*;
      6 
      7 public class client {
      8 	protected static int cexit = 0;
      9 	public static void main(String[] args) {
     10 		int port = 1234;
     11 		String ip = "127.0.0.1";
     12 		Socket connection;
     13 
     14 		ExecutorService exec = Executors.newSingleThreadExecutor();
     15 
     16 		try {
     17 			connection = new Socket(ip, port);
     18 			System.out.println("Connected Established");
     19 
     20 			exec.execute(new clienttask(connection));
     21 
     22 			DataInputStream inputStr = new DataInputStream(
     23 				new BufferedInputStream(connection.getInputStream()));
     24 
     25 			String serv_mes = "";
     26 			String s = "";
     27 			while (cexit == 0) {
     28 				if (inputStr.available() > 0) {
     29 					serv_mes = inputStr.readUTF();
     30 					System.out.print("\n" + serv_mes + "> ");
     31 				} else
     32 					Thread.sleep(500);
     33 			}
     34 			inputStr.close();
     35 			
     36 			// close the connection
     37 			connection.close();
     38 			exec.shutdown();
     39 
     40 		} catch (IOException ex) {
     41 			System.out.println("Client couldn't establish connection: " + ex);
     42 		} catch (InterruptedException ex) {
     43 			System.out.println("Sleep interrupted: " + ex);
     44 		}
     45 
     46 	}
     47 }