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 8583503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:33:14+00:00 2026-06-11T21:33:14+00:00

I am making a very simple app in which I send a text string

  • 0

I am making a very simple app in which I send a text string from java program on my PC and I am trying to receive same string from my android app in emulator.

This is my activity(server):

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_appstart);
    tv = (TextView) findViewById(R.id.textView1);
    try {
        DatagramSocket clientsocket = new DatagramSocket(9876);
        byte[] receivedata = new byte[1024];
        DatagramPacket recv_packet = new                   DatagramPacket(receivedata, receivedata.length);
        Log.d("UDP", "S: Receiving 1 sec...");
        clientsocket.receive(recv_packet);
        String rec_str = new String(recv_packet.getData());
        tv.setText(rec_str);
        Log.d(" Received String ", rec_str);
        InetAddress ipaddress = recv_packet.getAddress();
        int port = recv_packet.getPort();
        Log.d("IPAddress : ", ipaddress.toString());
        Log.d(" Port : ", Integer.toString(port));                                   clientsocket.close();
    } catch (Exception e) {
        Log.e("UDP", "S: Error", e);
    }
}

Here is my PC client java code:

  public static void main(String args[]) throws Exception
    {
            while(true)
    {           
    BufferedReader inFromUser = new BufferedReader(new       InputStreamReader(System.in));
            DatagramSocket clientSocket = new DatagramSocket();
            InetAddress IPAddress = InetAddress.getByName("127.0.0.1");
    System.out.println(IPAddress.getHostName());
            byte[] sendData = new byte[1024];
            byte[] receiveData = new byte[1024];
            String sentence = inFromUser.readLine();
            sendData = sentence.getBytes();
            DatagramPacket sendPacket = new    DatagramPacket(sendData,sendData.length,IPAddress,9876);
            clientSocket.send(sendPacket);
    System.out.println(sendPacket.getPort());
            clientSocket.close();
    }       
   }

However the app does not receive anything, but ot throws an exception on onReceive() function in android app….

Does any one knows why?

  • 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-11T21:33:15+00:00Added an answer on June 11, 2026 at 9:33 pm

    You get android.os.NetworkOnMainThreadException because you are running network methods on your main thread, this will make your app not responsive. Try putting it into a separate thread or…in this case, perhaps a Service that your Activity can bind to.
    (See How to fix android.os.NetworkOnMainThreadException? for an example)

    IF (NOT RECOMMENDED) you only need to test your UDP service, you can set your API requirement to 7 or below OR disable the network on main thread policy, you will not receive a exception. However, your app will be highly unresponsive.

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

Sidebar

Related Questions

I am making a simple app to learn Android at the same time, I
I was making this very simple lex program (just an introductory program). But on
I'm making a very simple 2D RPG in Java. My goal is to do
This should be simple. I'm making a very basic app, based on the Utility
I am making a very simple app in C# with Visual Studio, so I'm
I am making a very simple platform independent(at least that's the plan) console app.
I'm working on an app which takes in a raw binary message (very simple,
I have a table in my android app that is very simple. Has an
I have a very simple question to ask. I am making an app where
I'm trying to do a very simple program. It's actually a proxy, that I

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.