import java.net.Socket; import java.net.InetAddress; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.PrintWriter; import java.io.IOException; public class HelloClient { public static final int PORT = 2000; public static void main(String[] args) throws IOException { InetAddress addr; if (args.length >= 1) addr = InetAddress.getByName(args[0]); else addr = InetAddress.getByName(null); Socket socket = new Socket(addr, PORT); BufferedReader from_server = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter to_server = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); // true: PrintWriter is line buffered BufferedReader kbd_reader = new BufferedReader(new InputStreamReader(System.in)); String line_from_user; while (true) { System.out.print("Skriv en rad: "); line_from_user = kbd_reader.readLine(); to_server.println(line_from_user); String line_from_server = from_server.readLine(); if (line_from_server == null) break; System.out.println("Från servern: " + line_from_server); } } // main } // HelloClient