tags:

views:

180

answers:

1

Hi, ive implement simple udp server on my Android device.(sdk 1.5) it works fine when i am runnning a local client on the phone sends through it trigger to my server.

but when i try to get udp call from an outside server to my phone, it doesnt work. already make sure the outside server isnt blocked by firewall and it's sending the udp trigger to the right port, which my phone is listening to.

i used natstat on the phone and checked that the phone is realy listening to the it's local ip and the port ive setted it to.

here is my code of the server:(on the device)

    // server will listen to one client
    try
    {
        Thread udpServerThread = new Thread()
        {

            @Override
            public void run()
            {
                try
                {
                //   Retrieve the ServerName 

                            InetAddress serverAddr = InetAddress
                            .getByName("localhost");

                    Log.d("UDP", "S: Connecting...");
                    // Create new UDP-Socket 
                    socket = new DatagramSocket(SERVERPORT,serverAddr);






                    byte[] buf = new byte[17];


                    // * Prepare a UDP-Packet that can contain the data we
                    // * want to receive

                    DatagramPacket packet = new DatagramPacket(buf,
                            buf.length);
                    Log.d("UDP", "S: Receiving...");

                //   wait to Receive the UDP-Packet 
                    socket.receive(packet);
                    Log.d("UDP", "S: Received: '"
                            + new String(packet.getData()) + "'");


                    acceptedMsg=new String(packet.getData());


                    notifyService(acceptedMsg);



                    Log.d("UDP", "S: Done.");
                } catch (Exception e)
                {
                    Log.e("UDP", "S: Error", e);
                }


            }

        };
        udpServerThread.start();

    }

    catch (Exception E)
    {
     Log.e("r",E.getMessage())  ;
    }

so as i said, when i try it with local client(seperate thread) which sends udp trigger it works fine, but when i take this client implementation and put it on an outside real server, after UDP being sent, the phone doesnt respond to it.

any idea?

thanks,

ray.

A: 

At a guess, I'm assuming that when you call InetAddress.getByName("localhost"), you are getting the loopback address (127.0.0.1).

What you actually want to do is to have the socket bound to INADDR_ANY, which you can apparently achieve by creating your DatagramSocket like so:

socket = new DatagramSocket(SERVERPORT);
Hasturkun
thanks! it worked, but still cant understand what u meant by INADDR_ANY?
rayman
INADDR_ANY is the IPv4 wildcard address, binding a socket to INADDR_ANY is equivalent to binding to all of the system's addresses.
Hasturkun