java - ServerSocket do not work -
i beginner. run following code.
import android.app.activity; import android.os.bundle; import android.os.networkonmainthreadexception; import java.io.ioexception; import java.net.serversocket; import java.net.socket; public class mainactivity extends activity { serversocket server = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); try { server = new serversocket(4444); } catch (ioexception e) { e.printstacktrace(); } try { socket client = server.accept(); } catch (ioexception e) { e.printstacktrace(); } } }
but can see app stopped working message in emulator when run. found error occur on statement socket client = server.accept();. think statement wait until client request accepted. these user permissions added.
<uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.access_network_state"/>
i using android studio. , sdk 14.
you cant run network on ui thread , , think need while(1) when accept , try :
new thread(new runnable() { public void run() { try { socket client = server.accept(); } catch (ioexception e) { e.printstacktrace(); } } }).start();
Comments
Post a Comment