0

Help! I'm trying to do some simple java socket networking in Android Studio and I can't even get in the door. The server program seems to be working ok but the android app client crashes with the error "Unfortunately InTouch has stopped" while executing the line:

sock = new Socket("Mark2015jun08", 4415);

where the hostname I put in is what is reported by the C:>hostname command, the windows Networking Control Panel and it shows up in the DHCP table when I log into the router. It even crashes when I put in "" for a hostname which the documentation says should open a loopback. I've tried calling Socket() a dozen different ways as you can see from my code below. I could sure use some help. Thanks, -Mark

In the android client program I put the following try statement in the onResume() method:

try {
    //sock = new Socket("Mark2015jun08", 4415);
    byte[] ip = {(byte)192, (byte)168, (byte)1, (byte)124};
    //sock = new Socket(InetAddress.getByAddress(ip), 4415);
    //sock = new Socket(InetAddress.getByName("192.168.1.124"), 4415);
    //sock = new Socket("LOCALHOST", 4415);
    sock = new Socket("", 4415);
    // ommitted logic to set socket timeout and
    // open object streams based on sock
} catch (IOException e){
    sock = null;
    log(e.toString());
}

Both Windows ipconfig and the router DHCP table both confirm my ip address but I still get the same error. Three different sources all agree on my hostname but no matter what string I put in for hostname, even "", it doesn't work. What am I doing wrong?

I use the following permissions in my Manifest.xml file:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

This is my server program below created in Netbeans and I run it from the Windows cmd.exe window with the command C:>java -jar intouchserver.jar. This runs the server program on the laptop and it gets to the line

sock = serversock.accept();

and stops exactly as the documentation for accept() explains. As far as I can tell it's listening to port 4415. (Note: I checked serversock before this statement: it is non-null.)

package intouchserver;

import ...

public class InTouchServer {
    private static ServerSocket serversock;
    private static Socket sock;

    public static void main(String[] args) {
        try {
            serversock = new ServerSocket(4415);
            sock = serversock.accept();
        } catch (IOException e){
            System.out.println("Can't establish socket");
            System.out.println(e);
            System.exit(1);
        }
        // ommitted logic to open object streams based on sock
        // and read and write to them        
    }
}
luizfzs
  • 1,328
  • 2
  • 18
  • 34
mark777
  • 1
  • 2
  • The logcat output will give you a much more detailed stacktrace that contains the error information you need to debug this. – President James K. Polk Jul 27 '17 at 18:52
  • using `logcat` only helps if the `catch` block is executed which is not the case here. The client app simply ceases to be executed by the operating when it encounters the `sock = new...` statement. The only information I'm able to get regarding the error is the Android message:"Unfortunately application has stopped." And that's it. The only place to go from there is back to the home page – mark777 Jul 28 '17 at 21:24
  • 1
    No, I disagree. First off, don't catch exceptions in the way you are doing it. You want a full stacktrace, so make sure to log the full stacktrace. Secondly, any uncaught exceptions that cause Android to display "Unfortunately ... has stopped" will have their full stacktrace sent to the logcat facility. – President James K. Polk Jul 28 '17 at 22:07
  • Thanks so much for that tip about the logcat stacktrace. It shows: "java.lang.RuntimeException: Unable to resume activity" precisely at the sock=new... line in my Mainactivity. Any suggestions? – mark777 Jul 31 '17 at 17:36

1 Answers1

0

I solved my own problem. It turns out that if the Socket() call is in the onResume() function where I put it then you get a fairly useless error message but if you put the Socket() command in the onCreate() function you get a much more informative error: namely "NETWORK ON THE MAIN THREAD". So then putting the Socket() call in a thread solved the problem and off I went. THANK YOU so much James K Polk for the tip on logcat. I've been struggling with those "Application has Stopped" messages all along.

mark777
  • 1
  • 2