Android通过WiFi将UDP数据包发送到Java客户端

时间:2011-12-07 13:15:06

标签: java android udp wifi

我正在创建2个程序,其中一个程序从Adnroid设备发送UDP数据包,第二个程序(Java)接收它们。所以如果我使用WiFi路由器它会很好。但是如果我使用直接连接,Java应用程序就不会收到这些数据包。在直接连接下,我的意思是在计算机上创建HOTSPOT并通过Android设备连接到它。我使用了以下代码片段:

服务器代码:

public class UDPServer {
InetAddress groupAddress;
DatagramPacket packet;
byte[] buffer;
DatagramSocket socket;  
static String ip = "227.0.25.57";
static int port = 6789;

private boolean isRun = false;
private String message = "";
private int broadcastInterval;  
public void StopBroadcasting(){
    isRun = false;      
}
public void StartBroadcasting(String message, int broadcastInterval){
    isRun = true;
    this.message = message;
    this.broadcastInterval = broadcastInterval;     
    new Thread(runner).start();     
}
Runnable runner = new Runnable() {      
    @Override
    public void run() {
        while(isRun){               
            try {
                SendBroadcastMessage(message);
                System.out.println("msg sended...");
                Thread.sleep(broadcastInterval);
            } catch (InterruptedException e) {                  
                e.printStackTrace();
            } catch (IOException e) {                   
                e.printStackTrace();
            }
        }
        System.out.println("Stopping UDPServer...");
    }
};

public UDPServer() {
    buffer = new byte[4096];
    try {
        groupAddress = InetAddress.getByName(ip);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        socket = new DatagramSocket();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }                   
}

public void SendBroadcastMessage(String msg) throws IOException{
    if(msg==null) return;
    buffer = msg.getBytes();
    packet = new //DatagramPacket(buffer, buffer.length);
    DatagramPacket(buffer, buffer.length, groupAddress, port);
    socket.send(packet);
}   

public static void Send(String msg){
    try {                                   
        InetAddress group = InetAddress.getByName(ip);
        MulticastSocket s = new MulticastSocket(port);
        s.joinGroup(group);
        DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(),
        group, port);
        s.send(hi);
        System.out.println("send...");

        } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
}}

客户代码:

public class UDPClient {}
MulticastSocket socket;
InetAddress groupAddress;
DatagramPacket packet;
byte[] buffer;
static String ip = "227.0.25.57";
static int port = 6789;


public interface OnReceiveDataListener{
    public abstract void onReceiveData(String data);
}

private OnReceiveDataListener ReceiveDataListener = null;   

public void setReceiveDataListener(OnReceiveDataListener ReceiveDataListener) {
    this.ReceiveDataListener = ReceiveDataListener;
}

public OnReceiveDataListener getReceiveDataListener() {
    return ReceiveDataListener;
}

private boolean isRun = false;  
private Thread broadcastReceiver;
public void StopBroadcasting(){
    isRun = false;
    if(broadcastReceiver!=null)
        broadcastReceiver.interrupt();
}
public void StartBroadcasting(){
    isRun = true;           
    broadcastReceiver = new Thread(runner);     
    broadcastReceiver.start();
}
Runnable runner = new Runnable() {      
    @Override
    public void run() {
        while(isRun){               
            try {
                String msg = ReceiveBroadcastMessage();
                if(ReceiveDataListener!=null)
                    ReceiveDataListener.onReceiveData(msg);             
            } catch (IOException e) {                   
                e.printStackTrace();
            }
        }
    }
};

public UDPClient(){
    buffer = new byte[4096];
    try {
        groupAddress = InetAddress.getByName(ip);
        socket = new MulticastSocket(port);
        socket.joinGroup(groupAddress);
    } catch (UnknownHostException e) {          
        e.printStackTrace();
    } catch (IOException e) {           
        e.printStackTrace();
    }       
}

public String ReceiveBroadcastMessage() throws IOException{     
    packet = new DatagramPacket(buffer, buffer.length);
    System.out.println("before receive");
    socket.receive(packet);
    System.out.println(packet.getData());
    return new String(packet.getData());
}

public void DeInit(){
    try {
        socket.leaveGroup(groupAddress);
    } catch (IOException e) {

        e.printStackTrace();
    }
    socket.close();
}

MulticastSocket msocket;
public static void Receive(){
        MulticastSocket msocket;

        try {
        msocket = new MulticastSocket(port);
        InetAddress group = InetAddress.getByName(ip);
        msocket.joinGroup(group);

        byte[] inbuf = new byte[1024];
        DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);

        System.out.println("before receive");
        msocket.receive(packet);
        System.out.println("after receive");
        int numBytesReceived = packet.getLength();

        System.out.println(new String(packet.getData()));
        msocket.leaveGroup(group);
        msocket.close();
        System.out.println(numBytesReceived);
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }}

如果你能看到我有两种发送和接收数据包的方法。两者都不起作用!我错了什么? 请帮帮我。

1 个答案:

答案 0 :(得分:1)