Josey
Josey

Reputation: 33

Android UDP socket unable to send data

I'm newbie in Android programing, and I wanted to write a piece of code which simply send a string on 127.0.0.1 and I will be able to watch it on netcat but the problem is when I try it on my android project nothing happen, so I've tried in a usual java project and everything work well. So after a lot of research I've find... nothing

My android code :

   import android.app.Activity;
   import android.os.Bundle;
   import android.view.Menu;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.widget.Button;

   import java.net.*;
   import java.io.*;

   public class MainActivity extends Activity {


public static final int MON_PORT = 5001;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button bouton2 = (Button)findViewById(R.id.envoi);
    bouton2.setOnClickListener(new OnClickListener(){

      public void onClick(View v){    
  try {

        int port=MON_PORT;
        InetAddress adresse = InetAddress.getByName("localhost");
        DatagramSocket socket;


        String leMessage = "test";
        int longueur = leMessage.length();
        byte[] message= new byte [longueur];
        message = leMessage.getBytes();


        socket = new DatagramSocket();                      
        DatagramPacket packet = new DatagramPacket(message,longueur,adresse,port);
        socket.send(packet);
        System.out.println("test d'execution");
        socket.close();

            } catch (UnknownHostException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                        catch (SocketException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }   

      }
  });   


 }

And I've checked my AndroidManifest.xml and the permission :

<uses-permission android:name="android.permission.INTERNET"> </uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> </uses-permission>

are not between the applications tag

I use an AVD based on Android API 7 but I've tried on an another AVD with API 16 but results are the same, could someone help me to find what's wrong ?

Thanks

Upvotes: 3

Views: 8230

Answers (1)

Mr.Me
Mr.Me

Reputation: 9286

The actual problem is that your PC has no access to the UDP packet you are sending because that packet is being sent on a different IP address that device "emulator" uses through the Loopback mechanism.

Here is the thing. You can safely assume that the Android emulator is a remote machine that is remotely connected to your PC through an IP network, it has its own IP address and can communicate with your PC. You can't just use loopback IP to communicate with your PC over IP. Please use 10.0.2.2 instead as specified in this documentation page Emulator.

Using that IP you will be able to see the packets in netcat.

Upvotes: 2

Related Questions