/** * Fail DirServer.java * @author Jaanus Poial * @version 0.1 kevad 99 */ import java.io.*; import java.net.*; public class DirServer { public static void main (String [] parameetrid) { int pordinumber = 7654; // TCP-port if (parameetrid.length > 0) pordinumber = Integer.parseInt (parameetrid [0]); try { ServerSocket kuulaja = new ServerSocket (pordinumber); while (true) { new DirLoim (kuulaja.accept()); } } catch (IOException e) { System.out.println ("S/V viga: " + e); } } // main lopp } // DirServer lopp class DirLoim extends Thread { Socket pistik; // igal lo~imel oma pistik DirLoim (Socket p) { pistik = p; setPriority (NORM_PRIORITY - 1); // kuulamine on pisut ta"htsam start(); } public void run() { // katame yle try { InputStream sisse = pistik.getInputStream(); BufferedReader svoog = new BufferedReader (new InputStreamReader (sisse)); OutputStream valja = pistik.getOutputStream(); PrintWriter vvoog = new PrintWriter (new OutputStreamWriter (valja), true); String rida; if ((rida = svoog.readLine()) != null) { System.out.println ("Pordi " + pistik.getPort() + " kaudu saabus tellimus:\n" + rida); String vastus = annaVastus (rida); vvoog.println (vastus); System.out.println ("Pordi " + pistik.getPort() + " kaudu saadeti vastus:\n" + vastus); } pistik.close(); } catch (IOException e) { System.out.println ("S/V viga: " + e); } } // run lopp static String annaVastus (String tellimus) { File f = new File (tellimus); if (!f.exists() || !f.canRead()) return ("Ei saanud aru: " + tellimus); if (f.isDirectory()) { String [] nimekiri = f.list(); StringBuffer vastus = new StringBuffer(); for (int i=0; i