import java.net.*; import java.io.*; import java.util.Scanner; public class ClientSender { private boolean running = true; private Socket s = null; private PrintWriter out = 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 ClientSender(); } public ClientSender() { try { s = new Socket(host, port); System.out.println("CONNECTED TO: " + host + " - ON PORT: " + port); out = new PrintWriter(s.getOutputStream(), true); inputAndSend(); } catch (IOException ioe) { System.out.println("IOException generated: " + ioe); } } public void inputAndSend() { Scanner scanner = new Scanner(System.in); while(running) { System.out.print("Ange meddelande: "); String message = scanner.nextLine(); if(message.equals("exit")) { killMe(); } else { out.println(message); } } } public void killMe() { running = false; try { out.close(); s.close(); } catch(IOException ioe) { System.out.println("IOException generated: " + ioe); } System.exit(1); } }