title: "Connected" UDP socket receives ICMP Port Unreachable
body: <p>Based on the premise that UDP is a connectionless protocol I had presumed that whether a host is up or down would be irrelevant.</p>
  
  <p>However, now that I'm doing testing I've discovered that when I have "connected" my UDP client socket, a <code>write</code> to that socket returns an error because the server has sent back a ICMP Port Unreachable error ..</p>
  
  <p>The purpose of "connecting" the UDP port (According to Stevens <em>Unix Network Programming</em>) is to basically <em>caches</em> the entry from the routing table, rather than creating a new one for each packet, which is supposed to have performance benefits.</p>
  
  <p>However, this ICMP packet is causing me to lose my client socket, which is very annoying.</p>
  
  <p>Can anybody shed any light on why this could be? Are there any known workarounds?</p>
  
  <p>I'm using a 3p java library that doesn't account for this and just disconnects and I'll probably have to hack it to reconnect, but before I do I was kind of hoping there was something I could do at the (Linux) operating system level maybe to prevent this happening ... all investigations into socket options etc. have turned out to be unfruitful.</p>
  
  <p><em>EDIT</em></p>
  
  <p>In summary this isn't possible and fixing the code is the only way to do this.</p>
  
  <p>The only possibility appears to be configuring iptables to block the ICMP responses but that's a bit of a sledgehammer to crack this particular nut.</p>