• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Server/Datagram/Mutlicast sockets don't work

RazzleFnDazzle

Well-Known Member
I am running code on a real android device connected to wifi. I send out data on a multicast socket and wait for a response. I've tried it several different ways now and the phone can never receive a response. I tried opening a ServerSocket and manually sending data and that didn't work. I tried doing the same with a DatagramSocket and that didn't work. I tried to receive a response after sending data out on the MulticastSocket and that didn't work. It seems that I can't use my phone to receive data at all. I can send everything just fine, but can't receive. I can send multicasts and I can confirm the receiver is getting all the right ip addresses and stuff before responding. Here is a sample of the code:

phone snippet:
Code:
     MulticastSocket socket = null;
      WifiManager wifi = (WifiManager)gContext.getSystemService(Context.WIFI_SERVICE);
      MulticastLock lock = wifi.createMulticastLock("ptclock");
      try {
         lock.acquire();
         byte[] data = "hello".getBytes();
         InetAddress address = InetAddress.getByName(IP_ADDRESS);
         socket = new MulticastSocket();
         socket.joinGroup(address);
         socket.setSoTimeout(5000);

         while (true) {
            try {
               Thread.sleep(1000);
               DatagramPacket packet = new DatagramPacket(data, 1, address, PORT);
               socket.send(packet);

               data = new byte[1024];
               packet = new DatagramPacket(data, data.length);
               socket.receive(packet);
               //Never reaches this point due to timeout
            }
            catch (Exception e) {
                e.printStackTrace();
            }
         }
      }
      catch (Exception e) {
         e.printStackTrace();
      }
      finally {
         if (lock != null)
            lock.release();
         if (socket != null)
            socket.close();
      }
   }
computer snippet:
Code:
      MulticastSocket socket = null;
      try {
         byte[] data = new byte[1024];
         DatagramPacket packet = new DatagramPacket(data, data.length);
         InetAddress address = InetAddress.getByName(IP_ADDRESS);
         socket = new MulticastSocket(PORT);
         socket.joinGroup(address);

         while (true) {
            try {
               socket.receive(packet);
               //handles all the data correctly
               InetAddress retAddress = packet.getAddress();
               int retPort = packet.getPort();
               data = "goodbye".getBytes();
               packet = new DatagramPacket(data, data.length, retAddress, retPort);
                  socket.send(packet);
               }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
         }
      }
      catch (Exception e) {
            e.printStackTrace();
      }
      finally {
         if (socket != null)
            socket.close();
      }
I do have these permissions
Code:
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
 
Back
Top Bottom