Android multicast.receive()死亡,无异常

时间:2017-11-30 00:46:09

标签: java android

我正在为部分硕士学位制作多播信使。我之前已经成功地在桌面Java上实现了这个完全相同的实现,并且没有任何问题。但是,当我从Android运行代码时出现问题。我可以很好地发送到多播组,但接收不起作用。

输出只是在“2”后停在控制台中。

应用程序停止向我提供有关socket.receive()行的反馈,我也没有例外。

下面我附上了我用来接收邮件的课程。 对于完整的Android实现,我在以下位置提供了Dropbox链接:https://www.dropbox.com/s/vuoifbxqk01c079/MyApplication.zip?dl=0 它非常粗糙,但它确实成功发送,只是没有收到。

 public class EMMS extends Thread{
            MulticastSocket socket;
            InetAddress ip;
            byte [ ] buffer;
            int usedPort;
            public boolean isSending;
            private MainActivity view;
            public EMMS(MainActivity view, int port, String ipAddress){
                try {

                    // Create Multicast socket
                    socket = new MulticastSocket(port);
                    usedPort = port;
                    // Set address to join
                    ip = InetAddress.getByName(ipAddress);
                    // join group
                    socket.joinGroup(ip);
                    System.out.println("Group Joined! ");
                    // create buffer
                    buffer = new byte[512];
                    isSending = false;
                    this.view = view;
                }//end of try
                catch (SocketException se){
                    System.out.println("Socket Exception : " + se); }
                catch (IOException e) { System.out.println("Exception : " + e); }
            }

            public void run(){
                try {
                    System.out.println("Incoming Message! ");

                    while (isSending == false) {
                        // Create and receive packet
                        System.out.println("1");

                    DatagramPacket dp = new DatagramPacket(buffer, buffer.length,ip, usedPort);
                        System.out.println("2");
                        socket.receive(dp);
                        System.out.println("3");
                        String str = new String(dp.getData());
                        System.out.println("4");
                        String ipAddress = dp.getAddress().getHostAddress();
                        System.out.println("5");
                        // Create string array of components

                        String[] data = str.split("@@");
                        String senderName= data[2];
                        String sourceIP= ipAddress;
                        String typeOfEmergency=data[3];
                        String condition=data[0];
                        String instruction=data[1];
                        System.out.println("Incoming Message! ");
                        System.out.println(
                                "Sender: " + senderName + "\n" +
                                        "Source IP: " + sourceIP + "\n" +
                                        "Emergency Type: " + typeOfEmergency + "\n" +
                                        "Condition: " + condition + "\n" +
                                        "Instruction: " + instruction
                        );

                        // Create popup window
                        AlertDialog alertDialog = new AlertDialog.Builder(view).create(); //Read Update
                        alertDialog.setTitle("ALERT");
                        alertDialog.setMessage(
                                "Sender: " + senderName + "\n" +
                                "Source IP: " + sourceIP + "\n" +
                                "Emergency Type: " + typeOfEmergency + "\n" +
                                "Condition: " + condition + "\n" +
                                "Instruction: " + instruction
                        );

                        alertDialog.show();  //<-- See This!
                    }
                }//end of try
                catch (SocketException se){
                    System.out.println("Socket Exception : " + se); }
                catch (IOException e) { System.out.println("Exception : " + e); }


            }
            public void disconnect() throws IOException{
                socket.leaveGroup(ip);
            }

0 个答案:

没有答案