Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

android - UDP server doesn't accept calls from outside

I've implement simple udp server on my Android device.(sdk 1.5) it works fine when I am running 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 isn't 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 really listening to the its local ip and the port I've 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 doesn't respond to it.

any idea?

thanks,

ray.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...