One DatagramSocket, two different peers — the address travels in the packet
One DatagramSocket, two different peers — the address travels in the packet
Answer
try (DatagramSocket socket = new DatagramSocket(9000)) { byte[] reading = "t=21.4".getBytes(StandardCharsets.US_ASCII); socket.send(new DatagramPacket(reading, reading.length, dashboard, 9100)); socket.send(new DatagramPacket(reading, reading.length, archive, 9100)); }
Two destinations, one socket, no connection to either. In TCP this needs two `Socket` objects and two handshakes, because a `Socket` is bound to its peer for life. Here the socket knows only its own local port and every `DatagramPacket` carries its own address — which is the entire reason no `DatagramServerSocket` class exists.
Harold 4e ch12 §The UDP Protocol