0
我尝试在Android应用程序和服务器之间连续发送和接收UDP数据包。在Android上接收UDP数据包
有什么办法可以做到这一点吗?
我的当前网络代码(运行在一个线程中)如下所示。客户端通过3G连接。端口配置的客户端是1088.
服务器收到时仅向客户端回送数据包。 服务器从客户端正确接收数据包,但客户端没有收到任何回应。
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.d(TAG, "S: Connecting...");
DatagramSocket socket = new DatagramSocket();
DatagramSocket receive_socket = new DatagramSocket(SERVERPORT, InetAddress.getByName("0.0.0.0"));
while(running) {
DatagramPacket packet_send = new DatagramPacket(msg, msg.length, serverAddr, SERVERPORT);
Log.d(TAG, "C: Sending: '" + new String(msg) + "'");
socket.send(packet_send);
// Prepare a UDP-Packet that can contain the data we want to receive
DatagramPacket packet = new DatagramPacket(buf, buf.length);
Log.d(TAG, "S: Receiving...");
// Receive the UDP-Packet
receive_socket.receive(packet);
Log.d(TAG, "S: Received: '" + new String(packet.getData()) + "'");
synchronized (this) {
wait(500);
}
}
我怀疑3G连接是NAT(服务器报告一个不同于1088的端口)。 如果是这样,我能做些什么来克服它? 或者我的代码有什么问题吗?
服务器代码是Twisted UDP Echo示例:http://twistedmatrix.com/documents/current/core/howto/udp.html#auto1 – aberaud
服务器打印:>从213.55.xx接收到'来自客户端的Hello' .xx:6854 – aberaud
只是一个想法 - 但它会与android权限有关吗? – stephendwolff