import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPClient { public static void main(String args[]) { // goal: send "42" via UDP to myself, i.e. "localhost", on port 1027 int port = 1027; // create data array of size 1 (for demonstration) int size = 1; byte[] data = new byte[size]; data[0] = 42; try { // receiver address InetAddress receiver = InetAddress.getByName("localhost"); DatagramPacket packet = new DatagramPacket(data, size, receiver, port); DatagramSocket udp_service = new DatagramSocket(); udp_service.send(packet); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } } }