2121// distribute, sublicense, and/or sell copies of the Software, and to
2222// permit persons to whom the Software is furnished to do so, subject to
2323// the following conditions:
24- //
24+ //
2525// The above copyright notice and this permission notice shall be
2626// included in all copies or substantial portions of the Software.
27- //
27+ //
2828// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2929// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
3030// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -46,6 +46,17 @@ namespace BeardedManStudios.Forge.Networking
4646{
4747 public class CachedUdpClient : IDisposable
4848 {
49+ /// <summary>
50+ /// Winsock ioctl code for controlling whether the ICMP Port unreachable error should be disregarded or not.
51+ /// </summary>
52+ /// http://msdn.microsoft.com/en-us/library/cc242275.aspx
53+ /// http://msdn.microsoft.com/en-us/library/bb736550(VS.85).aspx
54+ /// https://stackoverflow.com/questions/7201862/an-existing-connection-was-forcibly-closed-by-the-remote-host/7478498#7478498
55+ /// uint IOC_IN = 0x80000000;
56+ /// uint IOC_VENDOR = 0x18000000;
57+ /// uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
58+ private const int SIO_UDP_CONNRESET = - 1744830452 ;
59+
4960 public const char HOST_PORT_CHARACTER_SEPARATOR = '+' ;
5061 private bool disposed = false ;
5162 private bool active = false ;
@@ -143,6 +154,7 @@ private void InitSocket(EndPoint localEP)
143154 recBuffer . SetSize ( 65536 ) ;
144155 }
145156
157+
146158 #region Close
147159 public void Close ( )
148160 {
@@ -226,6 +238,17 @@ public void Connect(string hostname, int port)
226238 }
227239 }
228240 }
241+
242+ /// <summary>
243+ /// Enable/disable whether the socket should disregard ICMP Port unreachable errors
244+ /// </summary>
245+ /// <param name="enabled"></param>
246+ public void IgnoreICMPErrors ( bool enabled )
247+ {
248+ // set socket to disregard ICMP errors.
249+ socket . IOControl ( SIO_UDP_CONNRESET , new byte [ ] { Convert . ToByte ( ! enabled ) } , null ) ;
250+ }
251+
229252 #endregion
230253 #region Multicast methods
231254 public void DropMulticastGroup ( IPAddress multicastAddr )
0 commit comments