public IPHandler() {
try {
_mc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
// Allows for multiple Multicast clients on the same host!
_mc.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, true);
_mc.Bind(new IPEndPoint(IPAddress.Any, mc_port));
// We need to add a group membership for all IPAddresses on the local machine
foreach(IPAddress ip in GetLocalIPAddresses()) {
/* the LAN address tends to throw exceptions in Vista 64, while
loopback doesn't... doing this, we make sure to at least get loopback
discovery. */
try{
_mc.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
new MulticastOption(mc_addr, ip));
}
catch {}
}
_mc.MulticastLoopback = true;
}
catch {
_mc = null;
ProtocolLog.WriteIf(ProtocolLog.Exceptions, "Unable to start listening on the" +
"multicast port, but we can still send and request services.");
}
_uc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
// It won't send to other mono applications on the same host otherwise...
_uc.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, true);
_uc.Bind(new IPEndPoint(IPAddress.Any, 0));
Interlocked.Exchange(ref _running, 1);
_listen_thread = new Thread(Listen);
_listen_thread.IsBackground = true;
_listen_thread.Start();
}