import java.net.*; import java.io.*; class ClientThread extends Thread { private final Socket socket; private final BufferedReader in; private final PrintWriter out; private final Storage storage; public ClientThread(Socket s, Storage m) throws IOException { socket = s; storage = m; in = new BufferedReader( new InputStreamReader( socket.getInputStream())); out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())), true); start(); } // ClientThread public void run() { try { while (true) { String inline = in.readLine(); if (inline == null) break; else if (inline.equals("LAGRA")) { String newString = in.readLine(); storage.setString(newString); out.println("OK"); } else if (inline.equals("HÄMTA")) { out.println(storage.getString()); } else out.println("NOK"); } // while } // try catch(IOException e) { } } // run } // class ClientThread class Storage { private String s; public synchronized void setString(String s) { this.s = s; } public synchronized String getString() { return s; } } // class Storage public class MultiServer { public static final int PORT = 2000; public static void main(String[] args) throws IOException { Storage storage = new Storage(); ServerSocket s = new ServerSocket(PORT); while(true) { Socket socket = s.accept(); ClientThread t = new ClientThread(socket, storage); } // while } // main } // MultiServer