import java.net.*; import java.io.*; public class M2ng{ public static void main(String argumendid[]) throws IOException{ ServerSocket ss = new ServerSocket(3001); while(true){ Yhendus y1 = new Yhendus(ss.accept()); y1.kirjuta("Tere tulemast! Oota teist kasutajat.."); Yhendus y2 = new Yhendus(ss.accept()); y2.kirjuta("Tere! Sind me ootasimegi! Mäng võib alata!"); y1.kirjuta("Hurraa! Sulle saabus partner!"); Diiler d = new Diiler(y1, y2); } } } class Yhendus{ PrintWriter võrku; BufferedReader võrgust; Socket sc; public Yhendus(Socket uus_sc){ sc = uus_sc; try{ võrku = new PrintWriter(sc.getOutputStream(), true); võrgust = new BufferedReader( new InputStreamReader(sc.getInputStream()) ); }catch(Exception e){e.printStackTrace();} } public void kirjuta(String lause) { System.out.println(lause); võrku.println(lause); } public String loe() throws IOException { return võrgust.readLine(); } public void close() { try{sc.close();}catch(Exception e){}; } } class Diiler extends Thread{ Yhendus y1, y2; public Diiler(Yhendus y1, Yhendus y2){ this.y1 = y1; this.y2 = y2; start(); } public boolean suhtle(Yhendus mina, Yhendus tema){ String rida = ""; try{ tema.kirjuta("Oota vastase käiku."); mina.kirjuta("Tee oma käik (lõpetamiseks \".\"):"); rida = mina.loe(); if(rida.equals(".") || (rida == null)) { tema.kirjuta("Vastane lahkus."); return false; } tema.kirjuta(rida); }catch(Exception e){e.printStackTrace();} return true; } public void run(){ try{ while(true){ if(!suhtle(y1, y2)) break; if(!suhtle(y2, y1)) break; } y1.close(); y2.close(); }catch(Exception e){e.printStackTrace();} } }