Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Main {
    private final static int BUFSIZE = 20;

    public static void main(String args[]) {
        try {

            int port = 80;

            DatagramSocket ds = new DatagramSocket(port);

            byte buffer[] = new byte[BUFSIZE];

            while (true) {

                DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
                // Receive data
                ds.receive(dp);
                // Display address from the datagram packet
                InetAddress ia = dp.getAddress();
                System.out.println(ia);

                buffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

                dp.setData(buffer, 2, 5);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}