import java.io.*; import java.net.*; import java.util.*; public class server{ static Vector kasutajad=new Vector(); static Date algaeg=new Date(); public static void main(String argumendid[]) throws IOException{ ServerSocket ss=new ServerSocket(3001); while(true){ kasutajad.add(new Loim(ss.accept())); } } public synchronized static void kirjutalog(String teade){ try{ PrintWriter faili=new PrintWriter(new FileWriter("opilased.txt",true)); faili.println(teade); faili.close(); }catch(Exception ex){ex.printStackTrace();} } public synchronized static void kirjutalog2(String teade){ try{ PrintWriter faili=new PrintWriter(new FileWriter("opilased.txt")); faili.println(teade); faili.close(); }catch(Exception ex){ex.printStackTrace();} } public synchronized static void trykilog(PrintWriter sihtkoht){ try{ BufferedReader failist= new BufferedReader(new FileReader("opilased.txt")); for(String rida=failist.readLine(); rida!=null;rida=failist.readLine()){ sihtkoht.println(rida); } failist.close(); }catch(Exception ex){ex.printStackTrace();} } public synchronized static void analyze(PrintWriter sihtkoht){ try{ BufferedReader failist= new BufferedReader(new FileReader("opilased.txt")); int loendur=0; String rida=failist.readLine(); while(rida!=null){ loendur++; rida=failist.readLine(); } sihtkoht.println(loendur); failist.close(); }catch(Exception ex){ ex.printStackTrace(); } } static class Loim extends Thread{ protected Socket sc; PrintWriter valja; String kasutajanimi=""; String IP; public Loim(Socket uus_sc){ try{ sc=uus_sc; valja=new PrintWriter(sc.getOutputStream(), true); IP=sc.getRemoteSocketAddress().toString(); start(); }catch(Exception e){e.printStackTrace();} } public void run(){ try{ BufferedReader sisse=new BufferedReader( new InputStreamReader(sc.getInputStream()) ); valja.println("Tere tulemast 6pilaste serverisse!"); valja.println("Sisesta kasutajanimi:"); kasutajanimi=sisse.readLine(); if(kasutajanimi.equals("1admin321")){ String rida=sisse.readLine(); if(rida.startsWith(".t")){trykilog(valja);} rida=sisse.readLine(); if(rida.startsWith(".s")){ //System.out.println(rida); rida=sisse.readLine(); // System.out.println(rida); kirjutalog2(rida.replaceAll("#", "\n")); Thread.sleep(1000); sc.close(); }}else{ kirjutalog(kasutajanimi+" "+IP); valja.print("Oled nr: "); analyze(valja); valja.println("Andmed sisestatud"); valja.println(); Thread.sleep(1000); sc.close();} }catch(Exception e){ System.out.println("Probleem: "+e); } try{sc.close();System.out.println("Lahkus kasutaja");}catch(Exception e){} kasutajad.remove(this); } } }