From 41447dc7e0110bebbfdc5fd841876985fe880060 Mon Sep 17 00:00:00 2001 From: Douglas Gillespie <50671166+douggillespie@users.noreply.github.com> Date: Fri, 5 Aug 2022 12:48:32 +0100 Subject: [PATCH] Multiport test Quick tester for multiport comms --- src/PamController/command/MultiportTest.java | 88 ++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/PamController/command/MultiportTest.java diff --git a/src/PamController/command/MultiportTest.java b/src/PamController/command/MultiportTest.java new file mode 100644 index 00000000..3d828066 --- /dev/null +++ b/src/PamController/command/MultiportTest.java @@ -0,0 +1,88 @@ +package PamController.command; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.MulticastSocket; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; + +/** + * Simple test of multiport communication. + * @author dg50 + * + */ +public class MultiportTest { + + private String addr = "224.100.20.20"; + private int port = 1290; + + private String tstStr = "ping"; + + private int BUFFLEN = 512; + private byte[] retBuffer = new byte[BUFFLEN]; + + public static void main(String[] args) { + new MultiportTest().run(); + } + + public MultiportTest() { + // TODO Auto-generated constructor stub + } + + private void run() { + InetAddress mcastaddr; + DatagramSocket socket = null; +// listInterfaces(); + try { +// mcastaddr = InetAddress.getByName(addr); +// InetSocketAddress group = new InetSocketAddress(mcastaddr, port); +// NetworkInterface netIf = NetworkInterface.getByName("bge0"); +// MulticastSocket socket = new MulticastSocket(port); +// socket.joinGroup(group, netIf); + socket = new DatagramSocket(); + socket.setSoTimeout(1000); + + DatagramPacket dataGram = new DatagramPacket(tstStr.getBytes(), tstStr.length(), + InetAddress.getByName(addr), port); + + socket.send(dataGram); + + DatagramPacket reGram = new DatagramPacket(retBuffer, BUFFLEN); + while (true) { + socket.receive(reGram); + String retStr = new String(reGram.getData(), 0, reGram.getLength()); + System.out.printf("\"%s\" received from %s %s port %d\n", retStr, reGram.getAddress(), reGram.getSocketAddress(), reGram.getPort()); + } + + // clean up +// socket.leaveGroup(group, netIf); + + } catch (IOException e) { + // TODO Auto-generated catch block + System.out.println(e.getMessage()); + } + socket.close(); + System.out.println("Done"); + + } + + private void listInterfaces() { + + try { + // there are 46 of these on my laptop, enumerated from 1. 0 is null. + for (int i = 0; i < 50; i++) { + NetworkInterface netIf = NetworkInterface.getByIndex(i); + System.out.printf("Interface %d : %s\n", i, netIf); + } + } catch (SocketException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + +}