0

I have a problem understanding why in Java a PortUnreachableException can be thrown from a DatagramSocket, when UDP itself is designed to be connection-less. My understanding of UDP is that packets will be dropped when they cannot be delivered. Why does the Java implementation of UDP with DatagramSocket provide feedback about packet delivery?

Florian
  • 332
  • 2
  • 15

1 Answers1

2

In this case, the "connection-less" refers to the fact that UDP does not do handshaking to set up its connection. and also, there is no acknowledgment of receipt of packets that are sent unless the server protocol has been designed to send them, so UDP does have connected sockets. Not the same thing.

While DatagramSocket has a connect method, the API states:

If the remote destination to which the socket is connected does not exist, or is otherwise unreachable, and if an ICMP destination unreachable packet has been received for that address, then a subsequent call to send or receive may throw a PortUnreachableException. Note, there is no guarantee that the exception will be thrown.

Therefore, it can be possible for you to send data to an address and have no indication there is not actually a connection.

it all depends and OS implementation for example in Linux there is a way to set up a connection to optimize routes http://man7.org/linux/man-pages/man7/udp.7.html#DESCRIPTION to enhance performance because of UDP doesn't the future routes so if connect is called beforehand it will be able to raise performance significantly you can read more about this in this answer wich look specifically about UDP in Linux

Karim
  • 1,004
  • 8
  • 18