Skip to content

Commit

Permalink
Add handling for SocketError.HostUnreachable in UDP discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
nwoolls committed Aug 25, 2014
1 parent e8722a7 commit a8a8ff4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
19 changes: 17 additions & 2 deletions MultiMiner.Discovery/Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,23 @@ public static void Send(IPAddress source, IPAddress destination, string verb, in
byte[] bytes = Encoding.ASCII.GetBytes(jsonData);

IPEndPoint ip = new IPEndPoint(destination, Config.Port);
client.Send(bytes, bytes.Length, ip);
client.Close();
try
{
client.Send(bytes, bytes.Length, ip);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.HostUnreachable)
//reasoning: we broadcast on all interfaces
//on OS X this may result in No route to host
Console.WriteLine(String.Format("{0}: {1}", source, ex.Message));
else
throw;
}
finally
{
client.Close();
}
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions MultiMiner.Remoting/Broadcast/Sender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System;

namespace MultiMiner.Remoting.Broadcast
{
Expand Down Expand Up @@ -34,8 +35,23 @@ private static void Send(IPAddress source, IPAddress destination, object payload
byte[] bytes = Encoding.ASCII.GetBytes(jsonPacket);

IPEndPoint ip = new IPEndPoint(destination, Config.BroadcastPort);
client.Send(bytes, bytes.Length, ip);
client.Close();
try
{
client.Send(bytes, bytes.Length, ip);
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.HostUnreachable)
//reasoning: we broadcast on all interfaces
//on OS X this may result in No route to host
Console.WriteLine(String.Format("{0}: {1}", source, ex.Message));
else
throw;
}
finally
{
client.Close();
}
}
}
}
Expand Down

0 comments on commit a8a8ff4

Please sign in to comment.