import java.net.*; import java.io.*; import java.util.Scanner; public class Client implements Runnable { private Thread thread = new Thread(this); private boolean running = true; private Socket s = null; private PrintWriter out = null; private BufferedReader in = null; private static String host = "atlas.dsv.su.se"; private static int port = 9494; private String currentInput = ""; public static void main(String args[]) { if(args.length != 0) { host = args[0]; if(args.length != 1) { port = Integer.parseInt(args[1]); } } new Client(); } public Client() { try { s = new Socket(host, port); System.out.println("CONNECTED TO: " + host + " - ON PORT: " + port); in = new BufferedReader(new InputStreamReader(s.getInputStream())); out = new PrintWriter(s.getOutputStream(), true); thread.start(); getInput(); } catch (IOException ioe) { System.out.println("IOException generated: " + ioe); } } public void run() { String inputLine = ""; while(running) { /* try { inputLine = in.readLine(); } catch(NullPointerException npe) { System.out.println("NullPointerException generated: " + npe); killMe(); } catch(IOException ioe) { System.out.println("IOException generated: " + ioe); killMe(); } System.out.println("\rMeddelande: " + inputLine); */ try {Thread.sleep(1000);} catch(Exception e){} //System.out.println("CURRENT INPUT: " + currentInput); } } public void getInput() { try { Runtime.getRuntime().exec("stty -icanon min 1").waitFor(); System.out.println("DONE THE MAGIC!"); } catch(InterruptedException ie) { System.out.println("InterruptedException generated: " + ie); } catch(IOException ioe) { System.out.println("IOException generated: " + ioe); } while(running) { currentInput = ""; try { /* BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in)); int c = 0; while((c = buffer.read()) != -1) { char character = (char) c; currentInput = currentInput + character; System.out.println("CURRENT INPUT CHAR: " + character); System.out.println("CURRENT INPUT STR : " + currentInput); } */ /* InputStreamReader isr = new InputStreamReader(System.in); char character = (char) isr.read(); System.out.println(character); currentInput = currentInput + character; System.out.println("CURRENT INPUT CHAR: " + character); System.out.println("CURRENT INPUT STR : " + currentInput); */ //InputStreamReader isr = new InputStreamReader(System.in); //char character = (char) System.in.read(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("En text ..."); String me = br.readLine(); System.out.print(me); byte[] b = new byte[1]; //int ii = System.in.read(b); /* if(!br.ready()) { try {Thread.sleep(1000);} catch(Exception e){} System.out.println("NOT READY!"); } else { String me = br.readLine(); System.out.println("DET HAR! " + me); } */ /* if(System.in.available() != 0) { System.out.println("DET FINNS MINST ETT TECKEN!"); if(br.ready()) { System.out.println("DET FINNS AVEN ETT RADBRYTNINGSTECKEN!"); } else { System.out.println("DET FINNS INTE ETT RADBRYTNINGSTECKEN!"); } } else { System.out.println("INTE ETT TECKEN!"); if(br.ready()) { System.out.println("DET FINNS AVEN ETT RADBRYTNINGSTECKEN!"); } else { System.out.println("DET FINNS INTE ETT RADBRYTNINGSTECKEN!"); } } */ //System.out.println(character); //currentInput = currentInput + character; //System.out.println("CURRENT INPUT CHAR: " + character); //System.out.println("CURRENT INPUT STR : " + currentInput); try {Thread.sleep(1000);} catch(Exception e){} } catch (IOException ioe) { System.out.println("IOException generated: " + ioe); } //currentInput = ""; //sendMessage(sentence); } } private void sendMessage(String message) { out.println(message); if(message.equals("exit")) killMe(); } public void killMe() { running = false; try { out.close(); in.close(); s.close(); } catch(IOException ioe) { System.out.println("IOException generated: " + ioe); } System.exit(1); } }