Reputation: 53
I want to communicate a TCP server and a client using sockets. I have a server wich has been tested and it runs properly, but when I try to implement the client in Java, it crashes when creating the socket and I don´t know why. Please, I would be very grateful if anyone could help me. Thanks. I put here the code of the simple app that I´ve made:
package udp.udp;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class UdpActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonSend = (Button)findViewById(R.id.send);
}
Button.OnClickListener buttonSendOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
try{
Socket s = new Socket("192.168.1.13", 5003);
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}};
}
and the fail log that I get:
05-22 19:37:47.035: D/AndroidRuntime(997): Shutting down VM
05-22 19:37:47.045: W/dalvikvm(997): threadid=1: thread exiting with uncaught exception
(group=0x409c01f8)
05-22 19:37:47.065: E/AndroidRuntime(997): FATAL EXCEPTION: main
05-22 19:37:47.065: E/AndroidRuntime(997): android.os.NetworkOnMainThreadException
05-22 19:37:47.065: E/AndroidRuntime(997): at
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
05-22 19:37:47.065: E/AndroidRuntime(997): at
java.net.InetAddress.lookupHostByName(InetAddress.java:391)
05-22 19:37:47.065: E/AndroidRuntime(997): at
java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
05-22 19:37:47.065: E/AndroidRuntime(997): at
java.net.InetAddress.getByName(InetAddress.java:295)
05-22 19:37:47.065: E/AndroidRuntime(997): at
udp.udp.UdpActivity$1.onClick(UdpActivity.java:55)
05-22 19:37:47.065: E/AndroidRuntime(997): at
android.view.View.performClick(View.java:3511)
05-22 19:37:47.065: E/AndroidRuntime(997): at
android.view.View$PerformClick.run(View.java:14105)
05-22 19:37:47.065: E/AndroidRuntime(997): at
android.os.Handler.handleCallback(Handler.java:605)
05-22 19:37:47.065: E/AndroidRuntime(997): at
android.os.Handler.dispatchMessage(Handler.java:92)
05-22 19:37:47.065: E/AndroidRuntime(997): at android.os.Looper.loop(Looper.java:137)
05-22 19:37:47.065: E/AndroidRuntime(997): at
android.app.ActivityThread.main(ActivityThread.java:4424)
05-22 19:37:47.065: E/AndroidRuntime(997): at
java.lang.reflect.Method.invokeNative(Native Method)
05-22 19:37:47.065: E/AndroidRuntime(997): at
java.lang.reflect.Method.invoke(Method.java:511)
05-22 19:37:47.065: E/AndroidRuntime(997): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-22 19:37:47.065: E/AndroidRuntime(997): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-22 19:37:47.065: E/AndroidRuntime(997): at dalvik.system.NativeStart.main(Native
Method)
05-22 19:37:47.635: I/dalvikvm(997): threadid=3: reacting to signal 3
05-22 19:37:47.655: I/dalvikvm(997): Wrote stack traces to '/data/anr/traces.txt'
05-22 19:42:47.176: I/Process(997): Sending signal. PID: 997 SIG: 9
Upvotes: 1
Views: 735
Reputation: 83301
You are attempting to perform a network access on the UI thread. Don't do that... it's bad.
Upvotes: 0
Reputation: 91
StrictMode was enforced by default starting with API-10. Check out this reference: http://developer.android.com/reference/android/os/StrictMode.html. StrictMode is a useful tool developers can use to identify blocking code on the main thread (blocking code on main thread causes ANRs and is bad in cases where you don't know how long it will be blocking such as network communication)
Upvotes: 1
Reputation: 84229
Looks like Android SDK insists on not doing networking on the main thread, see here.
Upvotes: 0