UDP服务器不接受来自外部的呼叫 [英] UDP server doesn't accept calls from outside
问题描述
我已经在Android设备上实现了简单的udp服务器.(SDK 1.5)当我在电话上运行本地客户端时,它可以正常工作,将触发器发送到我的服务器.
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.
但是当我尝试从外部服务器到我的电话的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 isn't 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 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()) ;
}
就像我说的那样,当我尝试使用发送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 doesn't respond to it.
有什么主意吗?
谢谢
射线.
推荐答案
我猜想是,当您调用 InetAddress.getByName("localhost")
时,您会得到回送地址(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屋!