Y
Y
Yu Yu2017-07-25 15:26:34
Android
Yu Yu, 2017-07-25 15:26:34

How to work with socket in android?

I'm trying to establish a connection between a PC and a phone using sockets.
Allow internet connections

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

I make the connection in a separate thread, which I create in the main activity by clicking on the button.
By the way, how is Runnable different from AsincTask?
final Handler handler = new Handler();
        handler.post(new Runnable() {
            Socket inSock = null;
            @Override
            public void run() {
                Socket sock = null;
                try{
                    sock = new Socket("192.168.1.2", 4444);
                }
                catch (Exception ex){
                    Log.v("Socket create", "Unable to create");
                    ex.printStackTrace();
                }
            }
        });

Connection is not happening (listening to sudo nc -l 4444)
Writes:
spoiler
07-25 12:09:10.425 1997-1997/study.sockettest V/Socket create: Unable to create
07-25 12:09:10.425 1997-1997/study.sockettest W/System.err: android.os.NetworkOnMainThreadException
07-25 12:09:10.425 1997-1997/study.sockettest W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1186)
07-25 12:09:10.425 1997-1997/study.sockettest W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-25 12:09:10.435 1997-1997/study.sockettest W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
07-25 12:09:10.435 1997-1997/study.sockettest W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:112)
07-25 12:09:10.435 1997-1997/study.sockettest W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-25 12:09:10.435 1997-1997/study.sockettest W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
07-25 12:09:10.435 1997-1997/study.sockettest W/System.err:     at java.net.Socket.startupSocket(Socket.java:596)
07-25 12:09:10.435 1997-1997/study.sockettest W/System.err:     at java.net.Socket.tryAllAddresses(Socket.java:127)
07-25 12:09:10.435 1997-1997/study.sockettest W/System.err:     at java.net.Socket.<init>(Socket.java:177)
07-25 12:09:10.435 1997-1997/study.sockettest W/System.err:     at java.net.Socket.<init>(Socket.java:149)
07-25 12:09:10.435 1997-1997/study.sockettest W/System.err:     at study.sockettest.MainActivity$2.run(MainActivity.java:80)
07-25 12:09:10.435 1997-1997/study.sockettest W/System.err:     at android.os.Handler.handleCallback(Handler.java:608)
07-25 12:09:10.446 1997-1997/study.sockettest W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 12:09:10.446 1997-1997/study.sockettest W/System.err:     at android.os.Looper.loop(Looper.java:156)
07-25 12:09:10.446 1997-1997/study.sockettest W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:4987)
07-25 12:09:10.446 1997-1997/study.sockettest W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
07-25 12:09:10.446 1997-1997/study.sockettest W/System.err:     at java.lang.reflect.Method.invoke(Method.java:511)
07-25 12:09:10.446 1997-1997/study.sockettest W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-25 12:09:10.446 1997-1997/study.sockettest W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-25 12:09:10.446 1997-1997/study.sockettest W/System.err:     at dalvik.system.NativeStart.main(Native Method)

Do I understand correctly that he is complaining about android.os.NetworkOnMainThreadException?
But I already seem to have created a separate thread.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2017-07-25
@xztau

You didn't create a thread. Handler.post delays execution of the Runnable to the next frame simply. It's on the same thread.
Great question, and it's too bad you didn't parse it yourself. Runnable is just a functional interface (an interface with a single run method).
AsyncTask is a more complex thing that just runs on a different thread.

P
Pavel, 2017-07-29
@Jeckit

AsyncTask is executed in another thread, or as the Background says.
And this Runnable is just, one might say, one run method that runs on the main thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question