import java.net.*; import java.io.*; import java.util.*; public class CompList { public static void main(String[] args) { ServerSocket serverSocket = null; int portNr = 4444; try { // Loome uue soketi, mis kuulab localhosti 4444 pordis serverSocket = new ServerSocket(portNr); } catch (IOException e) { System.err.println(e); e.printStackTrace(System.err); // kui socketi loomine ebaõnnestus, siis lõpetame programmi töö System.exit(1); } // computers sisaldab kõikide nende arvutite IP aadresse, // mis on selle programmi poole pöördunud Set computers = new HashSet(); while (true) { try { Socket s = serverSocket.accept(); InetAddress ip = s.getInetAddress(); computers.add(ip); } catch (IOException e) { System.err.println(e); e.printStackTrace(System.err); continue; } System.out.println("--------------------"); System.out.println("Serveri poole on pöördutud järgmistest arvutitest:"); Iterator it = computers.iterator(); while(it.hasNext()) { InetAddress ip = (InetAddress) it.next(); System.out.println("IP="+ip.getHostAddress() + " hostname="+ip.getHostName()); } System.out.println("--------------------"); } } }