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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:03:58+00:00 2026-06-11T06:03:58+00:00

I have an app which works with UDP packets. So I want to send

  • 0

I have an app which works with UDP packets. So I want to send and receive UDP packets at the same time in the same app. Is it real? Now it works perfectly, but separately.
Code snippet:
UDP Server:

public class UDPServer {    
DatagramPacket packet;  
DatagramSocket socket;      
static int port = 11111;    
private boolean isRun = false;
private String message = "";
private int broadcastInterval;  
private Context context;

public boolean IsRunUDPServer(){
    return isRun;
}
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() {      
    public void run() {
        while(isRun){               
            try {
                //Sending(message);
                SendBroadcastMessageOverWiFi(message);
                Thread.sleep(broadcastInterval);
                Log.e("SendBroadcastMessageOverWiFi", message);
            } catch (InterruptedException e) {                  
                e.printStackTrace();
            } catch (IOException e) {                   
            }
        }
        if(socket!=null){
            socket.disconnect();
            socket.close();             
        }           
    }
};

public UDPServer(Context context) {
    this.context=context;       
}

private InetAddress getBroadcastAddress() throws IOException {      
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();
    // handle null somehow
    if(dhcp==null){ return null; }
    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++)
      quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
    return InetAddress.getByAddress(quads);
}

private void SendBroadcastMessageOverWiFi(String message) throws IOException{
    InetAddress addr = getBroadcastAddress();
    if(addr!=null){
        if(socket==null){
            socket = new DatagramSocket(port);          
            socket.setBroadcast(true);              
        }
        DatagramPacket packet = new DatagramPacket(message.getBytes(), message.getBytes().length,
            addr, port);
        socket.send(packet);                
    }       
}}

UDP Client:

public class UDPClient {
MulticastSocket socket;
InetAddress groupAddress;
DatagramPacket packet;
byte[] buffer;

private static final int UDP_SERVER_PORT = 11111;
private static final int MAX_UDP_DATAGRAM_LEN = 32768;      

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 StopReceiving(){
    isRun = false;      
}
public void StartReceiving(){
    isRun = true;           
    buffer = new byte[4096];
    broadcastReceiver = new Thread(runner);     
    broadcastReceiver.start();
}
Runnable runner = new Runnable() {      
    @Override
    public void run() {
        while(isRun){               

            String lText;
            packet = new DatagramPacket(buffer, buffer.length);             
            DatagramSocket ds = null;
            try {
                ds = new DatagramSocket(UDP_SERVER_PORT);                   
                ds.receive(packet);
                lText = new String(packet.getData(), 0, packet.getLength());
                Log.i("UDP packet received", lText+"  "+packet.getLength()+"  "+packet.getData().length);
                if(getReceiveDataListener()!=null)
                    getReceiveDataListener().onReceiveData(lText);

            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (ds != null) {
                    ds.close();
                }
            }
        }           
    }
};}

If I start server, the client won’t be able receive udp packets.
Please help me. Thank you.

  • 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-11T06:03:59+00:00Added an answer on June 11, 2026 at 6:03 am

    Only one socket (in one process) can bind to a given UDP port at one time. It is not permitted to have two sockets bind to the same UDP port number.

    In most cases, there is no reason to have the client bound to a specific port. Instead, specify port 0 (INADDR_ANY) and the system will assign an unused dynamic port.

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

Sidebar

Related Questions

I have a DB in my app which normally works perfectly. Now, I have
I have created an iPhone app which works perfectly smooth on my simulator. Now
I have android app which works fine. And now I want to add some
I have an rss reader app which works perfectly on some feeds, but on
Have an ASP.net web app which works fine for a few days, but then
I have own component which works in my testing winform app good but when
I have an MVC3/EF4.1 app that I'm building which now works fine on my
I have an app which works fine, but slow on start up. I have
I have a rather large app which works in the simulator but creates in
i have used tweet sheet to tweet in my app,which works fine but can’t

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.