1

I have made a client-server application which can connect over the internet. I have hard coded in my client the server's IP which is fixed.

My question is that I want a way that will not take a lot of processing and memory. I want my client to check if internet is available and then try to connect to the server if its up and running. If not then to wait and try again. Keeping in mind that the app is supposed to always run on your computer 24/7 and connect when possible just like skype does, in the sense that its always on and when you have the internet available and server reachable , it connects.

My client's code that connects to the server:

private void connectToServer() throws Exception {
    showMessage("Attempting Connection... \n");
    try{
    connection = new Socket(InetAddress.getByName(serverIP), 6789);

    showMessage("Ok dude you are Connected to:" + connection.getInetAddress().getHostName());}
    catch(IOException e){
        int x = 0;
        while (true){
            try {
                showMessage("Sorry Your IP was not found, \nAutomatic detection:");
                showMessage("Now trying again");
                connection = new Socket(InetAddress.getByName(serverIP),6789);


                break;
            } catch (UnknownHostException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                showMessage("\nConnection not established, trying again\n");
            }
            /*x++;
            if(x>10) break;*/
        }
        //throw new Exception();
    }
}

This does work but takes a lot of processing and memory !

I know that by adding thread.sleep(5000), I am going to add them eventually etc... But this doesn't solve the memory problem. My program would be running for a long time and this doesn't stop the inevitable.

Is there a better way?

THANKS

P.S : showMessage() is just my function that displays the output on my GUI

The way I checked memory was using Window's Task Manager ! (the memory increases drastically)

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
  • This seems to be the problem `connection = new Socket(InetAddress.getByName(serverIP),6789);`. It's creating a new Socket connection object without clearing it even when the connection is unsuccessful that's why you have too many connection objects that is eating up your memory. Try finding a way to flush away the object when the connection isn't available. – Sky Jun 25 '14 at 03:36

2 Answers2

0

As you've noted in your question already, you should add a call to Thread.sleep() within the loop. As your code stands right now, it runs a tight endless loop that will be very taxing on the CPU.

As for memory usage, are you sure that the memory used is actually growing? The JVM may be allocating a large chunk of memory off the bat even for a simple program -- you should check to make sure that the usage is actually growing. If it is indeed a problem, it may just be because you're making a new Socket in each cycle of a tight loop, in which case adding the sleep() call would solve the issue by allowing the GC to take care of old Socket objects in time.

By the way, have you noticed that your code performs the exact same check again if the first one fails? Why not start the loop immediately and just perform the check once within the loop? This would allow you to significantly reduce your code's complexity. Look into the software guideline often referred to as "Don't Repeat Yourself" to learn more.

Jack O'Reilly
  • 434
  • 2
  • 14
  • I do see the repetition, Initially the code was actually looking for another alternate address. I changed it to make my question more understandable. Anyways thanks for pointing that out. – Bull Incarnadine Jun 25 '14 at 05:39
0

It seems you are having two problems

  1. How to check internet is available - please refer

Preferred Java way to ping an HTTP URL for availability

Detect internet Connection using Java

How to check if internet connection is present in java?

  1. Memory leak

You need to clean up the socket object if the connection fails.

Community
  • 1
  • 1
dgm
  • 2,287
  • 1
  • 23
  • 27
  • How is there a Memory leak? When the connection variable is made to point at a new socket every time, isn't the previous socket taken care of by the garbage collector? – Bull Incarnadine Jun 25 '14 at 05:35