Answer the question
In order to leave comments, you need to log in
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" />
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();
}
}
});
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)
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question