title: Android, how to obtain correct UDP address
body: <p>I have got a multicastsocket working on my android phone, I'm trying to connect one to another, one device is hosting as a wifi hotpot, and the other client will just connect, how do I find out the port that the client needs to listen to, to receive packets?</p>
  
  <p>Here is my server code</p>
  
  <pre><code>public class gameServer extends Thread{
  
  /**
   * Sets up a server for Android applciation
   */
  
  //Variables
  private static final String TAG = "gameServer";
  private int port = 50000;
  private MulticastSocket socket = null;
  private InetAddress address = null;
  private ArrayList&lt;gameObject&gt; assets = new ArrayList();
  
  //Client
  private DatagramPacket packet;
  
  
  public gameServer() throws IOException
  {
      //Setup server
      try
      {
          socket = new MulticastSocket( port );
          Log.d(TAG, "Server started");
      }
      catch ( IOException e)
      {
          Log.d(TAG, "Error code 0004");
          e.printStackTrace();
      }
  }
  
  public void passClient( gameObject clientTemp )
  {
      assets.add( clientTemp );
  }
  
  @Override
  public void run()
  {
      InetAddress temper = socket.getInetAddress();
      Log.d(TAG, "Server thread was started");
      System.out.println(temper);
      try {
          address = InetAddress.getByName("230.0.0.1");
      } catch (UnknownHostException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      }
      try {
          socket.joinGroup(address);
      } catch (IOException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      }
  
      while( true )
      {
      //Send out data
      try {
      //Setup the data to send to clients
      byte[] buf = new byte[256];
      String dataToBeSent = "";
  
      for(int i = 0;i &lt; assets.size(); i++)
      {
          dataToBeSent += assets.get(i).returnPosX() + ":" + assets.get(i).returnPosY();
      }
  
      buf = dataToBeSent.getBytes();
  
      // send it
      InetAddress group = InetAddress.getByName("230.0.0.1");
      DatagramPacket packet = new DatagramPacket(buf, buf.length, group, port);
      socket.send(packet);
      }
      catch (IOException e) 
      {
          e.printStackTrace();
      }
  
      //Receive data
      byte[] buf = new byte[256];
      //Set the properties of the packet (DatagramPacket)
      packet = new DatagramPacket(buf, buf.length);
      //Receive the data
      try {
          socket.receive(packet);
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      //Conver the data to a string
      String  received = new String(packet.getData(), 0, packet.getLength());
  
      //Debug
      //Log.d(TAG, received);
  
      }
  }
  }
  </code></pre>
  
  <p>Client code </p>
  
  <pre><code>public class gameClient extends Thread {
  
  private static final String TAG = "gameClient";
  private gameObject player;
  private Context context;
  private int port = 50000;
  private MulticastSocket socket;
  private InetAddress address = null;
  private DatagramPacket packet = null;
  
  public gameClient( gameObject temp )
  {
      player = temp;
      try {
          socket = new MulticastSocket( port );
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  public gameObject rtnObj()
  {
      return player;
  }
  
  @Override
  public void run()
  {
      //Gather ip address
      try {
          address = InetAddress.getByName("192.168.1.1");
      } catch (UnknownHostException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      }
  
      try {
          socket.joinGroup(address);
      } catch (IOException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      }
  
      while( true )
      {
          //Send out data
          try {
              //Receive data
              byte[] buf = new byte[256];
              //Set the properties of the packet (DatagramPacket)
              packet = new DatagramPacket(buf, buf.length);
              //Receive the data
              try {
                  socket.receive(packet);
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
              //Conver the data to a string
              String  received = new String(packet.getData(), 0, packet.getLength());
              String[] posValues = received.split(":");
  
  
              player.setPosition(Integer.parseInt(posValues[0]), Integer.parseInt(posValues[1]));
  
              //Debug
              Log.d(TAG, received);
  
          }
          finally
          {
  
          }
      }
  
  }
  
  }
  </code></pre>