Greenlight
Greenlight

Reputation: 195

What I receive with DatagramPacket?

If I use DatagramPacket class I can send and receive udp packets. Ok, but, can I be sure that I will receive only packets from the address I am sending packets to? Or I can receive udp packets on that (randomly assigned) port from any address? If so, I have to check the origin of every received udp packets and ignore not interesting packets.

Upvotes: 1

Views: 41

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123320

The sending and receiving is done through DatagramSocket and not DatagramPacket. A DatagramSocket can send data to arbitrary peers and receive data from arbitrary peers if it is not connected. To make sure that a DatagramSocket can only communicate (i.e. send and receive) with a single peer use DatagramSocket.connect.

Upvotes: 1

Related Questions