title: Communicate Android Application to PC Application over Wifi
body: <p>I am making an Android Application which will send an UDP Broadcast over Wifi. And there is a PC program to receive it. Here is the code:</p>
  
  <p>This is the user permission in the <code>AndroidManifest.xml</code></p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/&gt;
  </code></pre>
  
  <p><strong>DeviceManagerWindow.java</strong></p>
  
  <pre><code>import android.net.wifi.WifiManager;
  import android.os.Bundle;
  import android.app.Activity;
  import android.content.Context;
  import android.view.Menu;
  
  public class DeviceManagerWindow extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_device_manager_window);
          WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
          if(wifi != null)
          {
              WifiManager.MulticastLock lock = wifi.createMulticastLock("WifiDevices");
              lock.acquire();
          }
          Thread sendMulticast = new Thread(new MultiCastThread());
          sendMulticast.start();
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.device_manager_window, menu);
          return true;
      }
  
  }
  </code></pre>
  
  <p>This file sends multicast packets:</p>
  
  <p><strong>MultiCastThread.java</strong></p>
  
  <pre><code>public class MultiCastThread implements Runnable
  {
      MulticastSocket s;
      DatagramPacket pack;
      public MultiCastThread()
      {
          try
          {
              s = new MulticastSocket(WifiConstants.PORT_NO);
              s.joinGroup(InetAddress.getByName(WifiConstants.GROUP_ADDR));
          }
          catch(Exception e)
          {
              Log.v("Socket Error: ",e.getMessage());
          }
      }
      @Override
      public void run()
      {
          try
          {
              pack = new DatagramPacket(WifiConstants.WHO_IS.getBytes(),WifiConstants.WHO_IS.getBytes().length, InetAddress.getByName(WifiConstants.GROUP_ADDR), WifiConstants.PORT_NO);
              s.setTimeToLive(WifiConstants.TIME_TO_LIVE);
              s.send(pack);
          }
          catch(Exception e)
          {
              Log.v("Packet Sending Error: ",e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p><strong>WifiConstants.java</strong></p>
  
  <p>This file keeps records of the constants for Wifi Interaction.</p>
  
  <pre><code>public class WifiConstants
  {
      public static final int PORT_NO = 5432;
      public static final String GROUP_ADDR = "225.4.5.6";
      public static final int DGRAM_LEN = 1024;
      public static final String WHO_IS = "Who is?";
      public static final int TIME_TO_LIVE = 100;
  }
  </code></pre>
  
  <p>On my system I am running a java code to receive the packets and print it in the console.</p>
  
  <p><strong>ListenerDevice.java</strong></p>
  
  <pre><code>package Receiver;
  
  import java.net.DatagramPacket;
  import java.net.InetAddress;
  import java.net.MulticastSocket;
  
  
  public class ListenerDevice
  {
      public static final int PORT_NO = 5432;
      public static final String GROUP_ADDR = "225.4.5.6";
      public static final int DGRAM_LEN = 1024;
      public static final String WHO_IS = "Who is?";
      public static final int TIME_TO_LIVE = 1;
      public static void main(String[] args)
      {
          MulticastSocket socket = null;
          DatagramPacket inPacket = null;
          byte[] inBuf = new byte[DGRAM_LEN];
          try
          {
            //Prepare to join multicast group
            socket = new MulticastSocket(PORT_NO);
            InetAddress address = InetAddress.getByName(GROUP_ADDR);
            socket.joinGroup(address);
  
                while(true)
                {
                      System.out.println("Listening");
                      inPacket = new DatagramPacket(inBuf, inBuf.length);
                      socket.receive(inPacket);
                      String msg = new String(inBuf, 0, inPacket.getLength());
                      System.out.println("From :" + inPacket.getAddress() + " Msg : " + msg);
                }
          }
          catch(Exception ioe)
          {
              System.out.println(ioe);
          }
        }
  }
  </code></pre>
  
  <p>This is sending data, but not receiving on the PC end. both the devices are connected to the same network.</p>
  
  <p>I also created a class for receiving the packets on the mobile. and it is receiving the packets being sent.</p>
  
  <p>What should I do for connecting this.</p>