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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:44:06+00:00 2026-06-08T08:44:06+00:00

Im new to Android and Java programming. I have to do a bigger project

  • 0

Im new to Android and Java programming. I have to do a bigger project in the future witch includes some android developing. So I’ve watched some Videotutorials and now im trying to do some apps to train myself. Right now I want to make a chat service between two Android Devices ( Motorola Defy and Samsung Galaxy Tab 2 7.0) with UDP .

Here is my Code for both devices:

 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.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.TextView;

 public class udpserver 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;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.udpserver);

    IPAdresse = (EditText) findViewById(R.id.etIPAdresse);
    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);


    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");
        }



    }
}

}

and my UI looks like this(with translations )

enter image description here

my Problem now is : I can only receive for 1s at the start of the activty (1s = TIMOUT_MS). But I want to receive the whole time . Ive tried to use a while(true) loop , but the app always crashes with it.

Thanks in advance!!!
(And sorry for my English , I know its not the best 🙂 )

  • 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-08T08:44:09+00:00Added an answer on June 8, 2026 at 8:44 am

    What is happening is when you do while(true) you will get stuck in an infinite loop. That is why your app crushes. What you need to do is to start a new thread that will do the s.receive(p). Something like:

    new Thread(new Runnable() {
         public void run() {
    
           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());
            runOnUiThread(new Runnable() {
    
            @Override
            public void run() {
              RXtext.setText(text);
            }
        });
    
                    }
    }).start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Android programming however I do have some Java experience. What I
I am new to Android and Java programming. I have a class which implements
I'm new to Android & Java programming but have been programming in .NET for
I am new to Android/Java programming. I have two classes, one is an Activity
I am new to android programming and it's been some years since I have
I am new to Java programming / Android development - but I have been
I'm fairly new to Android programming and to Java in general, but I have
I'm new in Java and in programming on android. Have a look at this
I'm new to android and I have not done any Java programming for probably
I am new at android development and java programming, but I have decided to

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.