Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8168651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:35:50+00:00 2026-06-06T20:35:50+00:00

I am writing a small app in which android device tries to send a

  • 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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:35:52+00:00Added an answer on June 6, 2026 at 8:35 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small App in which i read some text from to console,
I'm writing small utility app (JBoss Netty based) which should perform some trivial login
I am writing small app, using Play Framework 2.0 which uses Ebean as ORM.
I am writing a small app ( initApp.js , initApp.routinj.js , initApp.controller.js ) which
I'm writing a small app to do a little processing on some cells in
I am doing a file server migration and I'm writing a small C# app
We are writing a corporate app which will run on a small number of
I've been writing a small Facebook app which uses the unified_thread table using FQL.
I'm writing an small ASP.NET MVC app, which allow read and comment for a
I´m writing a small prgramm in which the User is able to send small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.