import java.awt.Color; import java.awt.Font; import java.io.*; import java.net.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class Connection { private static Socket socket = null; private PrintWriter out = null; private BufferedReader in = null; private static String ip = null; private static int port = 0; private static JLabel statusLabel = new JLabel("idle"); public static void main(String[] args) throws IOException { statusLabel.setFont(new Font("Arial", 2, 14)); statusLabel.setForeground(Color.red); statusLabel.setBounds(0, 60, 500, 20); statusLabel.setVisible(true); getConfig(); createGUI(); } public static void createGUI() { JFrame frame = new JFrame("ServerTest"); frame.setLayout(null); JLabel portLabel = new JLabel("port = " + port); portLabel.setFont(new Font("Arial", 2, 14)); portLabel.setBounds(0, 0, 500, 20); portLabel.setVisible(true); JLabel ipLabel = new JLabel("ip = " + ip); ipLabel.setFont(new Font("Arial", 2, 14)); ipLabel.setBounds(0, 20, 500, 20); ipLabel.setVisible(true); JButton button = new JButton("Check"); button.setBounds(0, 100, 80, 20); button.setVisible(true); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Connect(); } }); frame.setVisible(true); frame.add(portLabel); frame.add(ipLabel); frame.add(button); frame.add(statusLabel); frame.setSize(250, 250); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void Connect() { try { SocketAddress sockaddr = new InetSocketAddress(ip, port); socket = new Socket(); socket.connect(sockaddr, 3000); setStatus(Color.green, "Online"); socket.close(); } catch (SocketTimeoutException e) { setStatus(Color.RED, "Offline"); } catch (IOException e) { setStatus(Color.RED, "No Network Connection"); } catch (IllegalArgumentException e) { setStatus(Color.RED, "Config invalid"); } } public static void getConfig() { try { BufferedReader in = new BufferedReader(new FileReader("server.cfg")); String str; while ((str = in.readLine()) != null) { String gesplit[] = str.split("="); for (int i = 0; i < gesplit.length; i++) { if (gesplit[i].contains("ip")) { ip = gesplit[1]; } if (gesplit[i].contains("port")) { port = Integer.parseInt(gesplit[1]); } } } in.close(); } catch (ArrayIndexOutOfBoundsException e) { setStatus(Color.RED, "Error"); } catch (IOException e) { setStatus(Color.RED, "Config not found"); } } public static void setStatus(Color kleur, String status) { statusLabel.setForeground(kleur); statusLabel.setText(status); } }