1

I'm new to android but not to sockets. I have a GUI with a button that runs the UAVServer thread. When I click it, the server should listen for the client. The line of code...

Socket client = serverSocket.accept();

...Should block until a client connects. But it just crashes saying "Unfortunately, DroidUAV has stopped."

public class UAVServer extends Thread {

    private String TAG = UAVServer.class.getSimpleName();   

    @Override
     public void run() {        
        ServerSocket serverSocket;
        try {
            serverSocket = new ServerSocket(12345);                                         
            Log.d(TAG, "Fails at the next line of code");
            Socket client = serverSocket.accept();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }
}

This seems pretty straight forward, why would it be crashing? Also how does android handle blocking ports, this might have something to do with it. If it was on the wifi, I figure it would be up to the router to unblock ports. But what if I was just on 4G?

NOTE: uses-permission android:name="android.permission.INTERNET" is in my manifest file.

Edit: I must be doing something wrong, because I just tried to make it run the client instead and it crashes on this line...

Socket s = new Socket("192.168.1.102",4444);

Edit: Added errors...

FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
    at android.view.View$1.onClick(View.java:3071)
    at android.view.View.performClick(View.java:3538)
    at android.widget.CompoundButton.performClick(CompoundButton.java:103)
    at android.view.View$PerformClick.run(View.java:14319)
    at android.os.Handler.handleCallback(Handler.java:608)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:156)
    at android.app.ActivityThread.main(ActivityThread.java:5099)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:991)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at android.view.View$1.onClick(View.java:3066)
    ... 12 more
Caused by: android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1190)
    at libcore.io.BlockGuardOs.accept(BlockGuardOs.java:54)
    at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:98)
    at java.net.ServerSocket.implAccept(ServerSocket.java:202)
    at java.net.ServerSocket.accept(ServerSocket.java:127)
    at com.example.droiduav.UAVServer.run(UAVServer.java:19)
    at com.example.droiduav.MainActivity.onToggleServer(MainActivity.java:31)
    ... 15 more
Ricky Casavecchia
  • 560
  • 3
  • 9
  • 28
  • 1
    Examine LogCat to see the Java stack trace associated with your crash. – CommonsWare Feb 24 '13 at 21:44
  • The likelihood is small since I see you're calling it from a `run` method, but it would blow up in that exact way if it were called from the UI thread. – Joachim Isaksson Feb 24 '13 at 21:57
  • I looked through LogCat and don't see anything useful. It starts a new thread when I click a button so it shouldn't freeze the UI. Also it crashes immediately. – Ricky Casavecchia Feb 24 '13 at 22:11
  • @rickster The red portion of the LogCat should tell you why it's crashing. You should post it here and also make sure that your network operations *are* done in a separate thread. – A--C Feb 24 '13 at 22:18
  • Added the errors from LogCat, I'm use C# which will actually give you useful error information. – Ricky Casavecchia Feb 24 '13 at 22:43

1 Answers1

4

Well, the problem you are facing has to do with the following exception that can be seen in your LogCat trace:

Caused by: android.os.NetworkOnMainThreadException

What it means is pretty straightforward guessable from the name. Don't perform any Network code on the Main UI Thread. Solution: Start your own thread for that (see this Android Developer article).

Why?

Network code can take a "long time" (assume several seconds) to execute. If you would call all your network code on the main thread, it will freeze the UI in the mean time (this is - as one could guess - not really preferable and could and will result in ANR exceptions).

see also these questions as they are related to your problem:

Community
  • 1
  • 1
Briareos386
  • 1,927
  • 18
  • 34
  • The UAVServer extends Thread, so I thought that the network code wasn't running on the Main thread right? The main thread kicks off the UAVServer thread but that shouldn't be a problem should it? – Ricky Casavecchia Feb 25 '13 at 18:14
  • 1
    How do you call your UAVServer? A guess into the blue: `UAVServer s = new UAVServer(); s.run()` -- well, if it's like that, it won't work. Try `UAVServer s = new UAVServer(); s.start();` instead ;) – Briareos386 Feb 25 '13 at 22:01
  • Your guess in the blue is right, I'll try it your way when I get home. – Ricky Casavecchia Feb 26 '13 at 16:31
  • I want to give props to GeneSys, he was right. http://www.coderanch.com/t/234040/threads/java/Difference-run-start-method-Thread – Ricky Casavecchia Feb 26 '13 at 21:42