Code example for DatagramPacket

Methods: getAddressgetDatagetLengthgetPort

public final class UDPUtil { 
	public static final Object udpReceiver(final int port) throws IOException {
		DatagramSocket ds = new DatagramSocket(port);
		byte[] buf = new byte[1024];
		DatagramPacket dp = new DatagramPacket(buf, buf.length);
		String str = new String(dp.getData(), 0, dp.getLength());
		System.out.println("IP:" + dp.getAddress().getHostAddress() + ",PORT:"
				+ dp.getPort());
		return null; 
	public static final void udpSender(final byte[] content,
			final String ipAdd, final int port) throws IOException {
		DatagramSocket ds = new DatagramSocket();
		DatagramPacket dp = new DatagramPacket(content, content.length,
				InetAddress.getByName(ipAdd), port);
Connect your IDE to all the code out there  Get Codota for Java