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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:46:45+00:00 2026-06-18T18:46:45+00:00

Hello I am trying to connect to a box that is on the network.

  • 0

Hello I am trying to connect to a box that is on the network. It has a working UDP server on it. With the code below I am able to communicate with the box and send/receive UDP packets from my phone. However, I cannot figure out how to setup using the android emulator. I’ve read a lot on StackOverflow as well as other forums with not luck. I am on windows 8

Android code:

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import android.os.Handler;
import android.util.Log;

public class udp implements Runnable {
    // Private variable
    private String IPServer = "10.0.2.2";           // For Emulator
    //private String IPServer = "255.255.255.255";  // For Phone
    private int portServer = 6286;
    private int portDestin = 4381;

    private InetAddress serverAddr;
    private InetAddress localAddr;
    private DatagramSocket socketSend;
    private DatagramSocket socketList;
    private DatagramPacket packetSend;
    private DatagramPacket packetList;
    private Handler uiHandler;

public udp(){
};

public void send() {

    // Retrieve the server name
    try {
        Log.d("UDP", "Creating InetAddress");
        serverAddr = InetAddress.getByName(IPServer);
    } catch (Exception e) {
        Log.e("UDP", "InetAddress Error:", e);
    }

    // Create UDP sockets
    try {
        Log.d("UDP", "Creating Sockets");
        socketSend = new DatagramSocket(portServer);
        socketList = new DatagramSocket(portDestin);
    }
    catch(Exception e) {
        Log.e("UDP", "DatagramSocket Error:", e);
    }

    // Sets socket to broadcast
    try {
        Log.d("UDP", "SetBroadcast");
        socketSend.setBroadcast(true);
    }
    catch(Exception e) {
        Log.e("UDP", "SetBroadcast Error:", e);
    }

    // Create UDP packets
    try {
        Log.d("UDP", "Creating packets");
        byte[] dataSend = new byte[32];
        byte[] dataRead = new byte[32];
        String msg = "Packet Broadcast";
        dataSend = msg.getBytes();
        packetSend = new DatagramPacket(dataSend, dataSend.length, serverAddr, portDestin);
        packetList = new DatagramPacket(dataRead, dataRead.length, serverAddr, portDestin);
    }
    catch(Exception e) {
        Log.e("UDP", "DatagramPacket Error:", e);
    }

    // Send packet
    try {
        Log.d("UDP", "Sending packet");
        socketSend.send(packetSend);
    }
    catch(Exception e) {
        Log.e("UDP", "Send Error:", e);
    }

    // Receive packet
    Log.d("UDP", "Receiving packet");
    for(int i=0; i<2; i++)
    {
        try {
            socketList.receive(packetList);
        }
        catch(Exception e) {
            Log.e("UDP", "Receive Error:", e);
        }
        String packetRec = new String(packetList.getData());
        Log.d("UDP", "Received: "+packetRec);
    }       

    socketSend.close();
    socketList.close();
}

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

I need to broadcast to the entire network; that is why Im using 255.255.255.255 but for android I read that it doesn’t work and I opened settings in the Emulator and found my IP to the one above.

I also port redir

telnet localhost 5554
redir add udp:4381:4381

log from phone:

02-11 12:01:09.743: D/UDP(17253): Creating InetAddress
02-11 12:01:09.743: D/UDP(17253): Creating Sockets
02-11 12:01:09.753: D/UDP(17253): SetBroadcast
02-11 12:01:09.753: D/UDP(17253): Creating packets
02-11 12:01:09.753: D/UDP(17253): Sending packet
02-11 12:01:09.753: D/UDP(17253): Receiving packet
02-11 12:01:09.753: D/UDP(17253): Received: Packet Broadcast????????????
02-11 12:01:09.763: D/UDP(17253): Received: ??????PACKETSTUFF???????????fP*

log from android emulator:

02-11 20:00:22.742: D/UDP(1201): Creating InetAddress
02-11 20:00:22.742: D/UDP(1201): Creating Sockets
02-11 20:00:22.752: D/UDP(1201): SetBroadcast
02-11 20:00:22.772: D/UDP(1201): Creating packets
02-11 20:00:22.772: D/UDP(1201): Sending packet
02-11 20:00:22.772: D/UDP(1201): Receiving packet
02-11 20:00:22.772: D/UDP(1201): Received: Packet Broadcast????????????????????

Thanks in advance

UPDATE:

I currently got it so that my emulator can send a packet outside the host computer using a UDP forwarder NetworkActiv AUTAPF. It sends out my UDP but it doesn’t not forward the response. Does anyone know where I should send the destination response to?

  • 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-18T18:46:47+00:00Added an answer on June 18, 2026 at 6:46 pm

    There is No Way you can connect your android emulator to any thing physical on your network directly.

    Android Emulator creates its own LAN and each emulator instance a new LAN instance with the same IPs (This is OK because they can’t access each other)

    If you insist on using the Emulator to communicate with the Box , you should create a UDP proxy socket on your PC (Using JAVA , C# , C++ , VB … or any desktop programming language you want)

    They way your Desktop App Will Work

    • get your LAN subnet to determain the packet source later.
    • Start a UDP Listener for the ports your server use.
    • Find the IP address of the sender (there are APIs for that) if the sender is the emulator, resend that packet to the box using the box IP

    There is a good demonistration on the subject on Android’s developers website.

    Please take a look Here

    Good Luck

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

Sidebar

Related Questions

I'm trying to connect to web server that uses HTTPS client certificate authentication. It
I'm trying to write a simple server that .NET clients can connect to to
I am trying to connect the sql server 2008 with servlet.this is my code.
Hello I am trying to connect my c# application I used this code I
Hello im trying to echo only the 50 letters, but something in my code
hello i am trying a sqlite database tutorial to build, but its not working
Hello I was trying to remove objects from object array that I have and
I am having trouble trying to connect to a secure OpenLDAP server which I
I am trying to use gmail's SMTP server smtp.gmail.com to send mails using C
I'm trying to connect to James server localhost, but I'm getting an exception javax.mail.MessagingException:

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.