~/ learn/ comp-348/ cards/ UDP: when losing packets is the right trade
1 of 5

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

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/175587e6-9194-46bc-af02-02b104582e02/flashcard utf-8 LF