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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:56:49+00:00 2026-06-13T13:56:49+00:00

here is my Code package com.example.messenger; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress;

  • 0

here is my Code

package com.example.messenger;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements View.OnClickListener {
    Button Send;
    EditText IPAdresse;
    EditText TEXT;
    TextView RXtext,tstep,rstep;
    private static final int TIMEOUT_MS = 1000;
    private static final int server_port = 13011;

    @SuppressLint({ "NewApi", "NewApi", "NewApi" })
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy); 

        IPAdresse = (EditText) findViewById(R.id.etIPAdresse);
        IPAdresse.setText("192.168.2.32");
        TEXT = (EditText) findViewById(R.id.etTEXT);
        Send = (Button) findViewById(R.id.bSendaa);
        RXtext = (TextView) findViewById(R.id.tvRXtext);
        tstep = (TextView) findViewById(R.id.tvTstep);
        rstep = (TextView) findViewById(R.id.tvRstep);
        Send.setOnClickListener(this);


        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                String text;
                byte[] message = new byte[1500];
                DatagramSocket s;

                while(true){
                try {
                    s= new DatagramSocket(server_port);
                    rstep.setText("1");
                    s.setBroadcast(true);
                    rstep.setText("2");
                    s.setSoTimeout(TIMEOUT_MS);
                    rstep.setText("3");

                    while(true){
                        DatagramPacket p = new DatagramPacket(message, message.length);
                        rstep.setText("4");
                        //InetAddress test = InetAddress.getByName("192.168.1.101");
                        //rstep.setText("5");
                        //s.connect(test,12345);
                        //rstep.setText("6");
                        s.receive(p);
                        rstep.setText("xxx");
                        text = new String(message, 0, p.getLength());
                        RXtext.setText(text);
                    }
                } catch (SocketException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    rstep.setText("fail socket create");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    rstep.setText("fail receive");
                }

                }
            }
        });
        }

public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){
    case R.id.bSendaa:
        tstep.setText("1");
        String messageStr= TEXT.getText().toString();
        tstep.setText("2");

        DatagramSocket s;
        try {
            s = new DatagramSocket();
            tstep.setText("3");
            s.setBroadcast(true);
            tstep.setText("4");
            s.setSoTimeout(TIMEOUT_MS);
            tstep.setText("5");
            InetAddress local = InetAddress.getByName(IPAdresse.getText().toString());
            tstep.setText("6");
            int msg_length=messageStr.length();
            tstep.setText("7");
            byte[] message = messageStr.getBytes();
            tstep.setText("8");
            DatagramPacket p = new DatagramPacket(message, msg_length,local,server_port);
            tstep.setText("9");
            s.connect(local,server_port);
            tstep.setText("10");
            s.send(p);
            tstep.setText("sending complete");


        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            tstep.setText("sending failed");
        }
    }
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

My android phone’s Ip is 192.168.2.32 , I saw it in wifi settings. So when I debug it row by row , I saw that my virtual device sent the packet , but I don’t know why my phone can’t get it. Can anybody help me?
Thanks
Regards

  • 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-13T13:56:50+00:00Added an answer on June 13, 2026 at 1:56 pm

    now it works , I didn’t call start() function for thread , and I should change the UI from runOnUiThread , but it worked only from device to device, not from Virtual Device to Device, so here is the working code

    package com.example.messenger;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.StrictMode;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    @SuppressLint("NewApi")
    public class MainActivity extends Activity implements View.OnClickListener {
        Button Send;
        EditText IPAdresse;
        EditText TEXT;
        TextView RXtext,tstep,rstep;
        private static final int TIMEOUT_MS = 10000;
        private static final int server_port = 13011;
        String mMessage;
    
        @SuppressLint({ "NewApi", "NewApi", "NewApi" })
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    
            StrictMode.setThreadPolicy(policy); 
    
            IPAdresse = (EditText) findViewById(R.id.etIPAdresse);
            IPAdresse.setText("192.168.2.32");
            TEXT = (EditText) findViewById(R.id.etTEXT);
            Send = (Button) findViewById(R.id.bSendaa);
            RXtext = (TextView) findViewById(R.id.tvRXtext);
            tstep = (TextView) findViewById(R.id.tvTstep);
            rstep = (TextView) findViewById(R.id.tvRstep);
            Send.setOnClickListener(this);
    
    
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
    
                    String text;
                    byte[] message = new byte[4];
                    DatagramSocket s;
    
                    try {
                        s= new DatagramSocket(server_port);
                        s.setSoTimeout(TIMEOUT_MS);
    
                        while(true){
                            try {
                                DatagramPacket p = new DatagramPacket(message, message.length);
                                s.receive(p);
    
                                mMessage = new String(message, 0, p.getLength());
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        RXtext.setText(mMessage);
                                    }
                                });
                            }
                            catch(Exception e) {
                                e.printStackTrace();
                            }
    
                        }
                    } catch (SocketException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        //rstep.setText("fail socket create");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        //rstep.setText("fail receive");
                    }
                }
    
            }).start();
        } 
    
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch(arg0.getId()){
        case R.id.bSendaa:
            String messageStr= TEXT.getText().toString();
    
            DatagramSocket s;
            try {
                s = new DatagramSocket();
                s.setBroadcast(true);
                s.setSoTimeout(TIMEOUT_MS);
                InetAddress local = InetAddress.getByName(IPAdresse.getText().toString());
                int msg_length=messageStr.length();
                byte[] message = messageStr.getBytes();
                DatagramPacket p = new DatagramPacket(message, msg_length,local,server_port);
                //s.connect(local,server_port);
                s.send(p);
                tstep.setText("sending complete");
    
    
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                tstep.setText("sending failed");
            }
        }
    }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the server code package echoserver; import java.net.*; import java.io.*; public class EchoServer
trying to find the current location of my device.code is here. package com.example.location; import
Here's my code package net.sourceforge.jwebunit.sample; import net.sourceforge.jwebunit.WebTestCase; public class JWebUnitSearchExample extends WebTestCase { public
package com.example.helloandroid; import java.util.ArrayList; import java.util.Arrays; import android.app.TabActivity; import android.os.Bundle; import android.view.Window; import android.widget.ArrayAdapter;
Started making a game. Here's some of my code. package games.tribe.screens; import games.tribe.model.World; import
I didn't understand ..why I am facing this error.. Here is my code: package
I am trying to install MySQLdb package. I found the source code here .
We have a very complex Oracle package here (over 4,100 lines of code) that's
I'm using meteor with the bootstrap package. (here is a chunk of code where
Trying to do a map example I found here http://codemagician.wordpress.com/2010/05/06/android-google-mapview-tutorial-done-right/ The problem is this

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.