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

  • SEARCH
  • Home
  • 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 8269829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:21:37+00:00 2026-06-08T06:21:37+00:00

I’ve write a server-client model android program. My pc acts as server and AVD

  • 0

I’ve write a server-client model android program. My pc acts as server and AVD acts as the client. I’m using 10.0.2.2 as the server’s address to connect, but the server side recieves nothing. My server works fine when the client is an usual java program. Problem occurs when the client is an Android emulator. I’ve been working on the problem for a really long time but I still have no solution. Hope someone could help me, thanks!
My code:
server:

import java.net.*;
import java.io.*;
public class Server {

    public static void main(String[] args) throws IOException{

        ServerSocket server = new ServerSocket(6002);
        while(true){
            Socket client = server.accept();
            BufferedReader in= new BufferedReader(new InputStreamReader(client.getInputStream())); 
            System.out.println(in.readLine());
        }
    }
}

client:

public class modules extends Activity {
    private ListView myListView;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.modules);
        myListView = (ListView)findViewById(R.id.listView1);

        Intent intent = getIntent();
        Bundle b = intent.getExtras();
        String semester = b.getString("semester");
        String subject = b.getString("subject");

        try {
            Socket socket = new Socket("10.0.2.2",6002);
            PrintWriter out=new PrintWriter(socket.getOutputStream(),true);  
            String s1 = "SEME" + semester;
            out.println(s1);
            out.flush();
            String s2 = "SUBJ" + subject;
            out.println(s2);
            out.flush();
            socket.close();

        } catch(UnknowHostException e) {
              e.printStackTrace();
        } catch(IOException e) {
               e.printStackTrace();
        }
    }
}

In logcat there are Errors like:

android.os.NetworkOnMainThreadException;

and also errors with Strict Mode:

07-22 14:24:24.892: E/AndroidRuntime(759):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099).

I have added the user-permission in the manifest xml file, so the problem must be something else.

  • 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-08T06:21:38+00:00Added an answer on June 8, 2026 at 6:21 am

    Look at what the error says: NetworkOnMainThreadException. It means that you should not do network operations on Android’s main thread because if your request happens to delay for a while responsiveness of the device will be blocked until that request is done.

    In order to use the Socket client on android you should give a look to AsyncTask, Thread or Service maybe. I would recommend you to use AsyncTask which is pretty straightforward. The code you have in your onCreate() method should be put in another class that extends AsyncTask this way:

    public class YourAsyncTask extends AsyncTask<Void, Void, Void>
    {
        protected Void doInBackground(Void... params)
        {
            //your socket code here...
        }
    }
    

    For more information check the docs for AsyncTask or browse the web/stackoverflow for complete examples!

    Hope this helps… bye.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but

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.