tags:

views:

1671

answers:

5

The below code results in a timeout.

It works fine on non-Android Java. What's the matter?

 //@Override
public static void run()
{
    //System.out.println ( "Local Machine IP : "+addrStr.toString (  )  ) ;
    HelloWorldActivity.tv.setText("Trace 1");

    try
    {
        // Retrieve the ServerName
        InetAddress serverAddr; //= InetAddress.getByName(Server.SERVERIP);
        InetAddress ias[] = InetAddress.getAllByName(Server.SERVERNAME);
        serverAddr  = ias[0];

        Log.d("UDP", "C: Connecting...");
        /* Create new UDP-Socket */
        DatagramSocket socket = new DatagramSocket();

        /* Prepare some data to be sent. */
        String strQuery="ÿÿÿÿgetservers"+" "+Server.iProtocol+" "+"'all'";
        Log.d("UDP", strQuery);
        //byte[] buf = ("ÿÿÿÿgetservers 68 'all'").getBytes();
        byte[] buf = strQuery.getBytes();

        /* Create UDP-packet with
         * data & destination(url+port) */
        DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, Server.SERVERPORT);

        Log.d("UDP", "C: Sending: '" + new String(buf) + "'");

        /* Send out the packet */
        socket.setSoTimeout(5000);
        socket.send(packet);
        Log.d("UDP", "C: Sent.");
        Log.d("UDP", "C: Done.");

        // http://code.google.com/p/android/issues/detail?id=2917

        byte[] buffer= new byte[1024*100];
        DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length); //, serverAddr, Server.SERVERPORT);
        socket.receive(receivePacket);
        HelloWorldActivity.tv.setText("TTT");

        String x = new String(receivePacket.getData());
        Log.d("UDP", "C: Received: '" + x  + "'");
        HelloWorldActivity.tv.setText(x);

   } catch (Exception e)
   {
       HelloWorldActivity.tv.setText(e.getMessage());
        Log.e("UDP", "C: Error", e);
   }

}


public class Server
{
    /*
    //public static java.lang.string SERVERIP;
    public static String SERVERNAME = "monster.idsoftware.com";
    public static String SERVERIP = "192.246.40.56";
    public static int SERVERPORT = 27950;
    public static int PROTOCOL = 68;
      */


        //public static String SERVERNAME="monster.idsoftware.com";
    public static String SERVERNAME="dpmaster.deathmask.net";

    public static String SERVERIP="192.246.40.56";
    public static int SERVERPORT=27950;
    //public static int iProtocol= 68; // Quake3
    public static int iProtocol=71; // OpenArena

}

Android manifest:

<?xml version="1.0" encoding="utf-8"?>

<use-permission id="android.permission.READ_CONTACTS" />


    <use-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_GPS" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
    <uses-permission android:name="android.permission.ACCESS_CELL_ID" />



 <uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />




<application
        android:icon="@drawable/icon"
        android:label="AAA New Application"
        >
    <activity android:name="HelloWorldActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

+1  A: 

Are you testing this on the emulator or on an actual phone? If you're using an emulator you need to be aware of how networking on the emulator works. Most specifically:

Each instance of the emulator runs behind a virtual router/firewall service that isolates it from your development machine's network interfaces and settings and from the internet. An emulated device can not see your development machine or other emulator instances on the network. Instead, it sees only that it is connected through Ethernet to a router/firewall.

You'll probably need to set up port forwarding, either using the Emulator console or using the adb command.

Dave Webb
it fails on the actual device, too...it always timeouts, in the emulator as well as in the actual device. It works fine on the development machine in a normal java environment. And I can access the internet on android...
Quandary
+1  A: 

byte[] buf = new byte[256]; socket = new DatagramSocket(port); DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet);

Above worked for me... Your buffer seems large?


Might be a little far fetched but what are you trying to receive from?

If you are trying to communicate with a XP machine that has two network cards (one could be wired and the other wireless, any mix) and you are are using XP built in firewall?

Then UDP requests are only listened for on the first network on the computer, disable other network cards on your system, only have enabled the one that your trying to talk your Android device to.

optics
large? 1024*100 = 100 kb. That's not much.I also tried on android directly, not the emulator, but same result there. I'll try with 256 bytes, however.
Quandary
Noting jarnbjo's comment the sample code above worked with my device connected via WiFi not via the cell network.
optics
I'll try that when I'm back home and have wifi
Quandary
still doesn't work
Quandary
+1  A: 

UDP works fine. i don't think your server is sending a response because your outgoing packet doesn't contain the bytes you think it contains.

see my comments in the android bug you raised (http://code.google.com/p/android/issues/detail?id=6163).

Elliott Hughes
A: 

I guess I am having the same issue as this. I have a UDP server on my Android device that will not receive UDP packets I send to it. Well, that's partially true. You see, if I connect to my LAN via WiFi the UDP packets get there. It's only when I am on Verizon's network that the UDP packets are never delivered. I contacted Verizon support to ask if UDP was blocked to my device and they said "no." However, I'm not confident they even knew what I was asking. Has anyone had success creating a UDP listener on an Android device on Verizon's network?

Lorek
A: 

Quandary, did you ever receive an answer?? It doesn't look like you did. I'm having the same problem using essentially the same code optics suggested:

DatagramSocket m_socket = new DatagramSocket(1581); byte[] buf = new byte[256]; DatagramPacket packet = new DatagramPacket (buf, buf.length); m_socket.receive(packet);
Rich
No, not yet. But now I have android 2.1 and new hope ;-))However, I have to rewrite the code first, because Vista x64 has managed to erase it while bluescreening.
Quandary