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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:37:59+00:00 2026-05-27T12:37:59+00:00

I’m creating 2 programs which one of them sends UDP packets from an Adnroid

  • 0

I’m creating 2 programs which one of them sends UDP packets from an Adnroid device and a second one (Java) receives them. So it works good if I use WiFi-router. But if I use a direct connection the Java application doesn’t receive these packets. Under the direct connection I mean creating HOTSPOT on computer and connecting to it by the Android device. I used code snippet bellow:

Server’s code:

public class UDPServer {
InetAddress groupAddress;
DatagramPacket packet;
byte[] buffer;
DatagramSocket socket;  
static String ip = "227.0.25.57";
static int port = 6789;

private boolean isRun = false;
private String message = "";
private int broadcastInterval;  
public void StopBroadcasting(){
    isRun = false;      
}
public void StartBroadcasting(String message, int broadcastInterval){
    isRun = true;
    this.message = message;
    this.broadcastInterval = broadcastInterval;     
    new Thread(runner).start();     
}
Runnable runner = new Runnable() {      
    @Override
    public void run() {
        while(isRun){               
            try {
                SendBroadcastMessage(message);
                System.out.println("msg sended...");
                Thread.sleep(broadcastInterval);
            } catch (InterruptedException e) {                  
                e.printStackTrace();
            } catch (IOException e) {                   
                e.printStackTrace();
            }
        }
        System.out.println("Stopping UDPServer...");
    }
};

public UDPServer() {
    buffer = new byte[4096];
    try {
        groupAddress = InetAddress.getByName(ip);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        socket = new DatagramSocket();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }                   
}

public void SendBroadcastMessage(String msg) throws IOException{
    if(msg==null) return;
    buffer = msg.getBytes();
    packet = new //DatagramPacket(buffer, buffer.length);
    DatagramPacket(buffer, buffer.length, groupAddress, port);
    socket.send(packet);
}   

public static void Send(String msg){
    try {                                   
        InetAddress group = InetAddress.getByName(ip);
        MulticastSocket s = new MulticastSocket(port);
        s.joinGroup(group);
        DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(),
        group, port);
        s.send(hi);
        System.out.println("send...");

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

Client’s code:

public class UDPClient {}
MulticastSocket socket;
InetAddress groupAddress;
DatagramPacket packet;
byte[] buffer;
static String ip = "227.0.25.57";
static int port = 6789;


public interface OnReceiveDataListener{
    public abstract void onReceiveData(String data);
}

private OnReceiveDataListener ReceiveDataListener = null;   

public void setReceiveDataListener(OnReceiveDataListener ReceiveDataListener) {
    this.ReceiveDataListener = ReceiveDataListener;
}

public OnReceiveDataListener getReceiveDataListener() {
    return ReceiveDataListener;
}

private boolean isRun = false;  
private Thread broadcastReceiver;
public void StopBroadcasting(){
    isRun = false;
    if(broadcastReceiver!=null)
        broadcastReceiver.interrupt();
}
public void StartBroadcasting(){
    isRun = true;           
    broadcastReceiver = new Thread(runner);     
    broadcastReceiver.start();
}
Runnable runner = new Runnable() {      
    @Override
    public void run() {
        while(isRun){               
            try {
                String msg = ReceiveBroadcastMessage();
                if(ReceiveDataListener!=null)
                    ReceiveDataListener.onReceiveData(msg);             
            } catch (IOException e) {                   
                e.printStackTrace();
            }
        }
    }
};

public UDPClient(){
    buffer = new byte[4096];
    try {
        groupAddress = InetAddress.getByName(ip);
        socket = new MulticastSocket(port);
        socket.joinGroup(groupAddress);
    } catch (UnknownHostException e) {          
        e.printStackTrace();
    } catch (IOException e) {           
        e.printStackTrace();
    }       
}

public String ReceiveBroadcastMessage() throws IOException{     
    packet = new DatagramPacket(buffer, buffer.length);
    System.out.println("before receive");
    socket.receive(packet);
    System.out.println(packet.getData());
    return new String(packet.getData());
}

public void DeInit(){
    try {
        socket.leaveGroup(groupAddress);
    } catch (IOException e) {

        e.printStackTrace();
    }
    socket.close();
}

MulticastSocket msocket;
public static void Receive(){
        MulticastSocket msocket;

        try {
        msocket = new MulticastSocket(port);
        InetAddress group = InetAddress.getByName(ip);
        msocket.joinGroup(group);

        byte[] inbuf = new byte[1024];
        DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);

        System.out.println("before receive");
        msocket.receive(packet);
        System.out.println("after receive");
        int numBytesReceived = packet.getLength();

        System.out.println(new String(packet.getData()));
        msocket.leaveGroup(group);
        msocket.close();
        System.out.println(numBytesReceived);
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }}

If you could see I have 2 methods of sending and receiving packets. The both don’t work! What do I wrong?
Help me please.

  • 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-05-27T12:37:59+00:00Added an answer on May 27, 2026 at 12:37 pm

    I’ve found solution:
    http://code.google.com/p/boxeeremote/wiki/AndroidUDP

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need 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.