Code example for DatagramPacket

Methods: getAddressgetDatagetLengthgetPort

0
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);
		ds.receive(dp);
		String str = new String(dp.getData(), 0, dp.getLength());
		System.out.println(str);
		System.out.println("IP:" + dp.getAddress().getHostAddress() + ",PORT:"
				+ dp.getPort());
		ds.close();
		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