简体   繁体   中英

Android TCP - program crashes

I cant seem to get a simple TCP connection going between a java server application and Android (I have tried both the emulator and the Android Dev Phone 2). I am getting this error on the Emulator "The application Data Receive (process com.mdog.datareceive) has stopped unexpectedly. Please try again."

Forgive me but I am very new to android. So I don't know how to debug it... but I am not trying anything too complex. Eventually I want to try and "consume" the bytes I am receiving in the application. and have the TCP run in the background... but for now simply getting the phone and computer to communicate would be great.

If you can help me that would be awesome.

Code for Android side:

public class Receive extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv = new TextView(this);

        Socket connectionSocket = null;
        byte[] inputHolderByteArray = new byte[5*1024];

        /* Connect to Server */
        try {
            connectionSocket = new Socket("192.168.0.104", 11313);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        /* Send an s to server to start transmission */
        try {
            PrintWriter out = new PrintWriter(connectionSocket.getOutputStream(), true);
            out.print('s');
            out.flush();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        /* read server transmission */
        try {
            connectionSocket.getInputStream().read(inputHolderByteArray);
        } catch (IOException e) {
            e.printStackTrace();
        }

        tv.setText("done");
        setContentView(tv);

    }

}

Each instance of the emulator runs behind a virtual router/firewall service that isolates it from your development machine's network interfaces and settings and from the internet.

The virtual router for each instance manages the 10.0.2/24 network address space — all addresses managed by the router are in the form of 10.0.2., where is a number. Addresses within this space are pre-allocated by the emulator/router.

You have to refer to the development machine with address as: 10.0.2.2 instead of 192.168.0.104 in your case. If you want to refer to another machine in your LAN, then you can Use Network Redirections

http://developer.android.com/guide/developing/tools/emulator.html#emulatornetworking

While superfell is correct that the full stack trace would help diagnose this, based on your code the/a likely problem is that you are breaking up every statement into it separate try/catch blocks. This probably isn't your core issue(my guess is you have a networking issue), but it is what is causing the system to crash.

Typically in Java, statements that are reliant on each other which can throw Exceptions are put in the same try/catch statement. What is most likely happening for you is that the code enters your first try catch block where you try to define a new socket. This fails throwing an exception like 'UnknownHostException'. connectionSocket remains null but the code enters the catch for UnknownHostException. You print the stack trace, but the program doesn't exit. Your code continues on to the following try/catch block where you call

 PrintWriter out = new PrintWriter(connectionSocket.getOutputStream(), true);

This causes a NullPointerException. This is a RuntimeException which is not checked and, because it is unchecked, you are not forced to catch it in a catch statement. The exception now causes your VM to crash and causes the error screen you have reported.

So, even though getting the logcat stacktrace will tell us more about your issue, the code you have constructed should be condensed into a single try/catch statement since all code is dependent on the first try/catch completing without error.

Edit:

Try constructing your application like this

public class Receive extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv = new TextView(this);

    Socket connectionSocket = null;
    byte[] inputHolderByteArray = new byte[5*1024];

    /* Connect to Server */
    try {
        connectionSocket = new Socket("192.168.0.104", 11313);
        PrintWriter out = new PrintWriter(connectionSocket.getOutputStream(), true);
        out.print('s');
        out.flush();
        connectionSocket.getInputStream().read(inputHolderByteArray);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    tv.setText("done");
    setContentView(tv);

}

}

When we say 'get the stacktrace', this means you need to connect to the emulator or device using the android debug bridge (adb) and a program called logcat. If you only have the emulator and no phone connected to your pc, try running the following: adb logcat *:D This will output the log information to the terminal. Leave this window open and run your application. You should see a stack trace get printed. Please take the time to get to know logcat and adb.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM