0

I'm working on my first Server/Client Project in Java. It's still very Basic and I was able to Exchange some data between the Server and the Client program. Now I facing Trouble to reconnect once the Client has terminated the Connection.

I'm comfing from Visual Basic where I just would have had a timer and a boolean, checking if a Connection was established or not and eventually reset the socket.

I tried something similar in Java by Setting up a Start method and a Restart method and just checking in a Loop what's the Status of the boolean.

Unfortunately, eclipse keeps giving me the message that I cannot make a static reference to a non-static field. Now I'm totally lost.

Here's the server's code which works fine once but cannot be restarted.

package ComplexChatServer;

public class MainRoutine {
    public Boolean boIsRunning;
    public ConnectionHandlerS chsEins;
    public Boolean boConnected = false;
    public String strText;

    public void StartRunning() {
        boIsRunning = true;
        chsEins = new ConnectionHandlerS();
        chsEins.SocketListener();
    }

    public void ContinueRunning() {
        boConnected = chsEins.getClientStatus();

        if (boConnected == true) {
            //System.out.println("Connected");
            strText = null;
            strText = chsEins.ReadInput();

            if (strText != null && strText.isEmpty() == false) {
                System.out.println("Loop");
                System.out.println(strText);
                strText = "";
                boIsRunning = true;
            }
            else if (strText.equalsIgnoreCase("+++END+++")) {
                boIsRunning = false;
                System.exit(0);
            }
        }
        else {
            //System.out.println("Not connected");
        }

    }

    public static void main (String [] args) {
        int intRun;

        while (true) {
            if (boIsRunning = true) {
                intRun = 1;
            }
            else {
                intRun = 0;
            }

            switch (intRun) {
            case 0:
                StartRunning();
                break;
            case 1:
                ContinueRunning();
                break;
            }
        }
    }
}
moffeltje
  • 4,521
  • 4
  • 33
  • 57
chris
  • 13
  • 3
  • I do see a mistake in your `if(boIsRunning = true)` because that will always evaluate to true (should be `==` or just `if(boIsRunning)...`) – moffeltje Oct 24 '17 at 21:07
  • 1
    Why are you calling System.exit(0)? That will exit the process entirely, and you'd have to restart the application. – Bill Horvath Oct 24 '17 at 21:07
  • Also, in Java it is a convention to start a method with lower case. – moffeltje Oct 24 '17 at 21:10
  • Thanks for the help. I will check out the answers given, also the link postet to the similar question. Three things in my defense ;-) 1. coming from visual basic, so I'm used to compare with = instead of ==; 2. I grew up in a german environment, so I'm used to start nouns with capital letters, thus the wrong camel casing; 3. at some point I might want to stop the server, therefore the System.exit. Thanks for your help. – chris Oct 25 '17 at 19:16

1 Answers1

1

You can not make a static call to a non-static member. A static member in Java is a member that belongs to a class itself; not belonging to its objects. So you either have to instantiate a MainRoutine object and call it's methods or turn your existing methods into static ones to be able to call them from your already static main method; depending on what you want to achieve.

Besides that, conventionally Java community uses camel casing when naming methods and variables. Please check the syntactic and logical correction below:

public static void main (String [] args) {
    MainRoutine routine = new MainRoutine();

    while(true) {
        if(boIsRunning) {
            routine.continueRunning();
        } else {
            routine.startRunning();
        }    
    }
}

Also as @Bill Horvath stated in his comment, notice that you're actually exiting the process rather than restarting it.

patateskafa
  • 149
  • 2
  • 12