I have a server class that runs and stops a server for my android app to connect to. It works fine when just using is from the terminal. But I want to make it a GUI so i don't have to launch it from anything else. And to keep a better output on it.

Heres my Main.java:

```
public class Main {
    public static void main(String[] args) throws Exception {
        Form f = new Form();
        f.setVisible(true);
    }
}

```

Form.java UPDATED:

```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Form extends JFrame {
    private JTextArea outputArea;
    private JButton button;

    private Server server; // Created a new Server instance
    private Thread t;      // Created a new Thread instance

    public Form() {
        initComponents();
        server = new Server(); // Initialized server
    }

    private void initComponents() {
        outputArea = new JTextArea();
        button = new JButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("Home Server");
        setSize(400, 350);
        setResizable(false);
        setLocationRelativeTo(null);

        button.setText("Start Server");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DoSomething();
            }
        });

        outputArea.setSize(400, 350);
        outputArea.setEditable(false);

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createSequentialGroup()
            .addComponent(button)
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
            .addComponent(button)
        );

        pack();
    }

    private void DoSomething() {
        if (!server.isRunning()) {
            t = new Thread(server); // Initialized thread.
            t.start();

            button.setText("Stop Server");
        } else {
            t.interrupt();

            button.setText("Start Server");
        }
    }
}

```

Server.java UPDATED (Removed static references):

```
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import java.util.Enumeration;

public class Server implements Runnable {
    private static ServerSocket serverSocket = null;
    private static Socket socket = null;
    private static DataInputStream dataInputStream = null;
    private static DataOutputStream dataOutputStream = null;

    private static int port = 19586;

    private static boolean running = false;

    public void Start() {
        try {
            System.out.println("Starting Server...");

            serverSocket = new ServerSocket(port);

            System.out.println("Server Started");
            System.out.println("IP Address: " + getIpAddress());
            System.out.println("Listening: " + serverSocket.getLocalPort());

            running = true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            System.out.println("Attempting to connect to clients...");
            socket = serverSocket.accept();

            dataInputStream = new DataInputStream(socket.getInputStream());
            dataOutputStream = new DataOutputStream(socket.getOutputStream());

            System.out.println("ip: " + socket.getInetAddress());
            System.out.println("message: " + dataInputStream.readUTF());

            dataOutputStream.writeUTF("connected");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Stop() {
        if (running) {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (dataInputStream != null) {
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (dataOutputStream != null) {
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            running = false;
        }
    }

    private String getIpAddress() {
        String ip = "";
        try {
            Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (enumNetworkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
                Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();
                while (enumInetAddress.hasMoreElements()) {
                    InetAddress inetAddress = enumInetAddress.nextElement();

                    if (inetAddress.isSiteLocalAddress()) {
                        ip += "SiteLocalAddress: " + inetAddress.getHostAddress();
                    }
                }
            }

        } catch (SocketException e) {
            e.printStackTrace();
            ip += "Something Wrong! " + e.toString() + "\n";
        }

        return ip;
    }

    public boolean isRunning() {
        return running;
    }

    public void run() {
        while (!Thread.interrupted()) {
            Start();
        }
    }
}

```

Im sure its a simple fix, but I just can't get it to terminate the server. I tried different methods and everything. I know that if I just call `Server.Start();` on the button press that it will use the current thread to start the server, and thats not what I want.

It will start, but Im not sure if it ever stops or not.