views:

157

answers:

2

Hi Everybody,

In my Android app, I try to connect to a port on a local server to get some packets. I've encased to code in some try & catch's but with the following code:

address = "192.168.175.82";
public void run() {
    try {
  smtpSocket = new Socket(address, 60001);

        os = new DataOutputStream(smtpSocket.getOutputStream());
        is = new DataInputStream(smtpSocket.getInputStream());
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: " + address);
        run();
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: " + address);
        run();
    } catch (Exception e) {
     System.out.println("retry");
     run();
    }

if ther server is not on, I obviously get the IOException a few times (e.g. a few hundred times) before I get a stack overflow error and the app crashes.

01-20 22:21:32.526: ERROR/AndroidRuntime(5678): java.lang.StackOverflowError
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at java.util.Hashtable.get(Hashtable.java:282)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at java.util.Properties.getProperty(Properties.java:177)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at java.lang.System.getProperty(System.java:440)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at java.lang.System.getProperty(System.java:412)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at java.lang.Boolean.getBoolean(Boolean.java:174)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at org.apache.harmony.luni.net.NetUtil$Action.run(NetUtil.java:89)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at org.apache.harmony.luni.net.NetUtil$Action.run(NetUtil.java:80)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at java.security.AccessController.doPrivilegedImpl(AccessController.java:264)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at java.security.AccessController.doPrivileged(AccessController.java:84)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at org.apache.harmony.luni.net.NetUtil.preferIPv6Addresses(NetUtil.java:51)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at java.net.Socket.startupSocket(Socket.java:777)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at java.net.Socket.tryAllAddresses(Socket.java:192)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at java.net.Socket.<init>(Socket.java:256)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at java.net.Socket.<init>(Socket.java:220)
01-20 22:21:32.526: ERROR/AndroidRuntime(5678):     at com.example.sockets.ReadSocket.run(ReadSocket.java:83)

Could anyone please point me in the right direction of how to get this to work? Perhaps I should test the connection first somehow (if so, please say how?) Thanks

Chris

A: 

You can try isConnected() and isBound() on your socket to check wether you are connected or not before trying to get an inputstream.

Sephy
Thanks for the reply! However, doing this still causes the overflow error. I even tried removing the i/o streams altogether but the error persisted.
Chris Robinson
the 2 method return true???because if they don't, you can change your handling, or set a delay before repeating the attempt to connect?
Sephy
+2  A: 

For starters, drop the recursive call. This will get rid of the stack overflow. Replace it with a loop and sleep in each unsuccessful iteration

  public void run() {
     boolean connected = false;  
     while(!connected)
     {
     try {
      smtpSocket = new Socket(address, 60001);

            os = new DataOutputStream(smtpSocket.getOutputStream());
            is = new DataInputStream(smtpSocket.getInputStream());


      connected = true;
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: " + address);

    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: " + address);

        } catch (Exception e) {
         System.out.println("retry");

        }
        if (!connected)
            Thread.sleep(1000)
      }
    }
hrstrand
Many thanks. Just figured this out as you posted! Excuse my idiotic-ness =P.
Chris Robinson