How can I read ICMP errors from “connected” UDP sockets?
If the target machine discards the message because there is no process reading on the requested port number, it sends an ICMP message to your machine which will cause the next system call on the socket to return ECONNREFUSED. Since delivery of ICMP messages is not guarenteed you may not recieve this notification on the first transaction. Remember that your socket must be “connected” in order to receive the ICMP errors. I’ve been told, and Alan Cox has verified that Linux will return them on “unconnected” sockets. This may cause porting problems if your application isn’t ready for it, so Alan tells me they’ve added a SO_BSDCOMPAT flag which can be set for Linux kernels after 2.0.0. 5. How can I be sure that a UDP message is received? You have to design your protocol to expect a confirmation back from the destination when a message is received. Of course is the confirmation is sent by UDP, then it too is unreliable and may not make it back to the sender. If the sender does not get confir