UDP服务器犯规接受来自外线电话 [英] UDP server doesnt accept calls from outside

查看:85
本文介绍了UDP服务器犯规接受来自外线电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

香港专业教育学院执行我的Andr​​oid设备上简单的UDP服务器。(SDK 1.5) 它工作正常,当我捉迷藏的本地客户端上的电话发送通过它触发到我的服务器。

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.

但是当我试图让UDP的呼叫从外部服务器到我的手机,它不工作。 已经确保通过防火墙阻止外部服务器的心不是和它的发送UDP触发正确的端口,我的手机被监听。

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.

我用手机上natstat,并检查该手机是真的听它的本地IP和端口香港专业教育学院设置好的它。

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.

这是我的$ C $服务器的C:(设备上)

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

因此​​,正如我说,当我尝试用本地客户端(单独的​​线程),它发送的UDP触发它工作正常,但是当我把这个客户端实现,并把它放在一个外部的真实服务器上, 发送UDP后,手机犯规应对它。

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.

任何想法?

感谢

射线。

推荐答案

在猜测,我假设,当你调用 InetAddress.getByName(本地主机) ,你所得到的环回地址(127.0.0.1)。

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

你真正想要做的是有一个绑定到INADDR_ANY,你可以明显地通过创建的DatagramSocket 像这样实现插座:

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

这篇关于UDP服务器犯规接受来自外线电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆