import java.net.ServerSocket; import java.net.Socket; 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 HelloServer { public static final int PORT = 2000; public static void main(String[] args) throws IOException { ServerSocket s = new ServerSocket(PORT); System.out.println("Servern: Lyssnar..."); Socket socket = s.accept(); System.out.println("Servern: Uppkoppling accepterad."); BufferedReader from_client = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter to_client = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); // true: PrintWriter is line buffered while (true) { String inline = from_client.readLine(); System.out.println("Servern: Tog emot '" + inline + "'"); // Not: inline == "quit" if (inline == null || inline.equals("quit")) break; to_client.println("HELLO, CLIENT! YOU SAID: " + inline); } } // main } // HelloServer