Code example for DatagramPacket

Methods: getAddress, getData, getLength, getPort

0
	MessageInfo nextMessage() throws IOException { 
		byte[] receiveData = new byte[1024];
 
		Log.i(TAG, "Waiting for a message.");
 
		DatagramPacket receivePacket = new DatagramPacket(receiveData,
				receiveData.length);
 
		appSocket.receive(receivePacket);
		Log.i(TAG, "Received a packet.");
 
		InetAddress sourceIPAddress = receivePacket.getAddress();
		Log.d(TAG, "Source IP Address: " + sourceIPAddress);
		 
		int sourcePort = receivePacket.getPort();
		 
		String strPacket = new String( receivePacket.getData(), 
                						0,  
                						Math.min( receivePacket.getLength(), receivePacket.getData().length),
                						"US-ASCII"); 
		Log.d(TAG, "Data is " + strPacket);
		 
		MessageInfo message = gson.fromJson(new String(strPacket), MessageInfo.class);