private void run() throws UnknownHostException { while (true) { try { serverSocket = new DatagramSocket(new Random().nextInt(1000) + 8000); break; } catch (SocketException e) { e.printStackTrace(); } } portListener = new PortListener(); portListener.start(); periodicConnectionCheck = new PeriodicConnectionCheck(); periodicConnectionCheck.start(); parent = new JFrame("Chat"); parent.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); parent.setMinimumSize(new Dimension(300, 400)); parent.setLocationRelativeTo(null); this.gui = new GUI(); parent.getContentPane().add(this.gui, BorderLayout.CENTER); parent.pack(); parent.setVisible(true); showLoginDialog(); while (parent.isVisible()) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } }
public static void main(String[] args) { main = new Main(); try { main.run(); } catch (UnknownHostException e) { e.printStackTrace(); } finally { portListener.interrupt(); if (Main.main.serverSocket != null) Main.main.serverSocket.close(); } }