import java.net.*; import java.io.*; public class ClientReceiver { private boolean running = true; private Socket s = null; private BufferedReader in = null; private static String host = "127.0.0.1"; private static int port = 2000; public static void main(String args[]) { if(args.length != 0) { host = args[0]; if(args.length != 1) { port = Integer.parseInt(args[1]); } } new ClientReceiver(); } public ClientReceiver() { try { s = new Socket(host, port); System.out.println("CONNECTED TO: " + host + " - ON PORT: " + port); in = new BufferedReader(new InputStreamReader(s.getInputStream())); receiveAndShow(); } catch (IOException ioe) { System.out.println("IOException generated: " + ioe); } } public void receiveAndShow() { 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("Meddelande: " + inputLine); } } public void killMe() { running = false; try { in.close(); s.close(); } catch(IOException ioe) { System.out.println("IOException generated: " + ioe); } System.exit(1); } }