无法从其他Android设备通过Android接收UDP数据包

时间:2015-02-26 10:56:53

标签: java android sockets networking

我是Android的新手,我正在使用服务器/客户端技术的UDP数据包在Android中开发示例聊天应用程序。我有一个Android客户端,通过它我可以从Android发送数据包,并可以在java上接收相同的数据包,但是当我尝试通过Android接收相同的数据包时,它没有收到任何数据包。我使用的是广播地址192.168.0.255,这是我网络的广播地址。如果我能够在java上收到数据包为什么我无法在android上接收数据包? 任何实现相同目标的想法都将受到赞赏。我的服务器代码是:

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class MyDatagramReceiver extends Thread {
    private boolean bKeepRunning = true;
    private String lastMessage = null;
    private Handler mHandler = null;
    private DatagramSocket datagramsocket = null;
    DatagramPacket packet = null;
    private static InetAddress Serverip = null;
    public final static int port = 13011;

    public MyDatagramReceiver(Handler handler) {

        this.mHandler = handler;
        Log.v("MyDatagramReceiver", "Handler Initialised");

    }

    public void run() {
        String message = null;
        InetAddress ip = null;

        byte[] lmessage = new byte[10];

        try {
            Serverip = getDeviceIP();
            Log.v("MyDatagramReceiver", "DeviceIp:" + Serverip);
            // datagramsocket = new DatagramSocket(port);
            datagramsocket = new DatagramSocket(port, Serverip);

            datagramsocket.setBroadcast(true);
            datagramsocket.setSoTimeout(50000);
            Log.v("MyDatagramReceiver", "Socket CREATED");

            while (bKeepRunning) {
                // lmessage = new byte[10];
                packet = new DatagramPacket(lmessage, lmessage.length);
                Log.v("MyDatagramReceiver", "RECEIVING PACKET CREATED");
            datagramsocket.receive(packet);
            Log.v("MyDatagramReceiver", "PACKET RECEIVED");

            message = new String(lmessage);
            Log.v("MyDatagramReceiver", "Received packet contains:"
                    + message);

            ip = packet.getAddress();
            Log.v("MyDatagramReceiver", "IP" + ip);
            lastMessage = message;
            Log.v("MyDatagramReceiver", "lastmessage contains:"
                    + lastMessage);
            String var = message;
            Message completeMessage = mHandler.obtainMessage();
            completeMessage.obj = var;
            Log.v("MyDatagramReceiver", "completeMessage" + completeMessage);
            completeMessage.sendToTarget();
            byte[] m = "Hello_client".getBytes();
            InetAddress aHost = InetAddress.getLocalHost();

            DatagramPacket request = new DatagramPacket(m, m.length, ip,
                    port);
            datagramsocket.send(request);
            Log.v("MyDatagramReceiver", "Packet sent");
        }
        if (datagramsocket != null) {
            datagramsocket.close();
        }

    } catch (Throwable e) {
        e.printStackTrace();
    }

}

public void kill() {
    bKeepRunning = false;
}

public String getLastMessage() {
    return lastMessage;
}

public static InetAddress getDeviceIP() {
    if (Serverip != null) {
    } else {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress intAdress = enumIpAddr.nextElement();
                    if (!intAdress.isLoopbackAddress()) {
                        if (intAdress instanceof Inet4Address) {
                            Serverip = intAdress;
                            Log.d("ServerIpHelper", "Client Ip Address: "
                                    + Serverip.getHostAddress().toString());
                            return Serverip;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return Serverip;
}

}

1 个答案:

答案 0 :(得分:2)

最后我自己找到了答案。上面的代码工作得很好,只是防火墙阻塞了UDP数据包..当我关闭数据包开始流入..