MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

import javax.swing.JPanel;

public class MainClass extends JPanel {

    public static void main(String[] args) {

        try {
            String data = "data in UDP";
            byte[] buffer = data.getBytes();

            DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
                    new InetSocketAddress("localhost", 5002));

            DatagramSocket socket = new DatagramSocket(5003);

            System.out.println("Sending a packet...");
            socket.send(packet);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}