0

I am writing a small app in which android device tries to send a packet to server(which is my own computer).

I get some error, I dont know how to fix it.

Here is the error:

>07-02 14:58:56.125: E/AndroidRuntime(17976): FATAL EXCEPTION: main
>07-02 14:58:56.125: E/AndroidRuntime(17976): android.os.NetworkOnMainThreadException
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >libcore.io.IoBridge.connectErrno(IoBridge.java:127)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >libcore.io.IoBridge.connect(IoBridge.java:112)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >java.net.Socket.startupSocket(Socket.java:566)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >java.net.Socket.tryAllAddresses(Socket.java:127)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at java.net.Socket.<init>(Socket.java:177)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at java.net.Socket.<init>(Socket.java:149)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >com.amazon.hsyal.WifiSimulatorActivity$1.onClick(WifiSimulatorActivity.java:33)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >android.view.View.performClick(View.java:3536)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >android.view.View$PerformClick.run(View.java:14130)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >android.os.Handler.handleCallback(Handler.java:605)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >android.os.Handler.dispatchMessage(Handler.java:92)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at android.os.Looper.loop(Looper.java:137)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >android.app.ActivityThread.main(ActivityThread.java:4495)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >java.lang.reflect.Method.invokeNative(Native Method)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >java.lang.reflect.Method.invoke(Method.java:511)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at >com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
>07-02 14:58:56.125: E/AndroidRuntime(17976):   at dalvik.system.NativeStart.main(Native >Method)

My android program is:

    public class WifiSimulatorActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Button send_packet_button = (Button) findViewById(R.id.send_packet_button);
        send_packet_button.setOnClickListener(sendPacketListener);

    }
    Button.OnClickListener sendPacketListener = new Button.OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub
            try{
                Socket s = new Socket("192.168.52.138", 12345);
                OutputStream out = s.getOutputStream();
                PrintWriter output = new PrintWriter(out);
                BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                output.write("Sending packet 1");

                s.close();
            }
            catch(UnknownHostException e){
                e.printStackTrace();
            }
            catch(IOException e){
                e.printStackTrace();
            }
        }

    };
}

    public static void main(String[] args){
    try{
    Boolean end = false;
    ServerSocket ss = new ServerSocket(12345);
    System.out.println("Program running");
    while(!end){
            //Server is waiting for client here, if needed
            Socket s = ss.accept();
            BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
            PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
            String st = input.readLine();
           System.out.println("Tcp Example From client: "+st);
            output.println("Good bye and thanks for all the fish :)");
            s.close();

    }
ss.close();
}
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
Harshit Syal
  • 649
  • 2
  • 11
  • 22

2 Answers2

2

android.os.NetworkOnMainThreadException

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.

In your case You are running Network operation from MainUI thread that's why you got this Exceptions (In android never allowed that). Use Android AsyncTask and put your Network (Socket code) in doInBackGround() of AsyncTask.

Look at SO question android.os.NetworkOnMainThreadException

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
  • With help of your answer that error is fixed but still my program is not working, when ever I create server socket and watch the socket address by calling getLocalSocketAddress, I see: 0.0.0.0/0.0.0.0:53074 – Harshit Syal Jul 02 '12 at 11:31
1

As docs says: android.os.NetworkOnMainThreadException :

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

So try to implement your Network relative code by Using AsyncTask, Handler, HandlerThread and runOnUiThread

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213