/// <summary> /// Constructor that binds this object instance to an IPEndPoint. If you need to change settings dynamically, Dispose and recreate a new object. /// </summary> /// <param name="endPoint">IPEndPoint where to send the multicast packets -- should be in range 224.0.0.0 to 239.255.255.255</param> /// <param name="timeToLive">ushort Time To Live of the packets -- how many routers will we cross -- set to 2 for local or testing</param> public UdpSender(System.Net.IPEndPoint endPoint, ushort timeToLive) { this.endPoint = endPoint; LstSocks.Socket.SockInterfacePair sip = LstSocks.Socket.GetSharedSocket(endPoint); this.sock = sip.sock; this.externalInterface = sip.extInterface; if (Utility.IsMulticast(endPoint.Address)) { SocketOptionLevel sOL = SocketOptionLevel.IP; if (endPoint.AddressFamily == AddressFamily.InterNetworkV6) { sOL = SocketOptionLevel.IPv6; } // Set the TTL sock.SetSocketOption(sOL, SocketOptionName.MulticastTimeToLive, timeToLive); // Enable Multicast Loopback sock.SetSocketOption(sOL, SocketOptionName.MulticastLoopback, 1); } else { // Enable Unicast Loopback echoEndPoint = new IPEndPoint(externalInterface, endPoint.Port); } }
/// <summary> /// Constructor that binds this object instance to an IPEndPoint. If you need to change IPEndPoint dynamically, Dispose and recreate a new object. /// </summary> /// <param name="endPoint">IPEndPoint where we should be listening for IP Multicast packets.</param> /// <param name="TimeoutMilliseconds">Milliseconds before lack of a packet == a Network Timeout</param> /// <example> /// ... /// MulticastUdpListener mcListener = new MulticastUdpListener(endPoint1); /// mcListener.Receive(packetBuffer); /// mcListener.Displose(); /// /// MulticastUdpListener mcListener = new MulticastUdpListener(endPoint2); /// mcListener.Receive(packetBuffer); /// mcListener.Displose(); /// /// mcListener = null; /// ... /// </example> public UdpListener(System.Net.IPEndPoint endPoint, int timeoutMilliseconds) { LstSocks.Socket.SockInterfacePair sip = LstSocks.Socket.GetSharedSocket(endPoint); this.sock = sip.sock; this.externalInterface = sip.extInterface; this.ep = endPoint; lock (sip) { if (!sip.Initialized) { try { // Set the timeout on the socket if (timeoutMilliseconds > 0) { sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeoutMilliseconds); } // Set the socket to send & receive from this endpoint sock.Bind(new IPEndPoint(externalInterface, endPoint.Port)); // Make room for 80 packets plus some overhead sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 1500 * 80); if (Utility.IsMulticast(endPoint.Address)) { if (endPoint.AddressFamily == AddressFamily.InterNetworkV6) { // Join the IPv6 Multicast group sock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(endPoint.Address)); } else { // Join the IPv4 Multicast group sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(endPoint.Address)); } } sip.Initialized = true; } catch { this.Dispose(); throw; } } } }
/// <summary> /// Constructor that binds this object instance to an IPEndPoint. If you need to change settings dynamically, Dispose and recreate a new object. /// </summary> /// <param name="endPoint">IPEndPoint where to send the multicast packets -- should be in range 224.0.0.0 to 239.255.255.255</param> /// <param name="timeToLive">ushort Time To Live of the packets -- how many routers will we cross -- set to 2 for local or testing</param> public UdpSender(System.Net.IPEndPoint endPoint, ushort timeToLive) { this.endPoint = endPoint; LstSocks.Socket.SockInterfacePair sip = LstSocks.Socket.GetSharedSocket(endPoint); this.sock = sip.sock; this.externalInterface = sip.extInterface; if ( Utility.IsMulticast( endPoint.Address ) ) { SocketOptionLevel sOL = SocketOptionLevel.IP; if (endPoint.AddressFamily == AddressFamily.InterNetworkV6) { sOL = SocketOptionLevel.IPv6; } // Set the TTL sock.SetSocketOption(sOL, SocketOptionName.MulticastTimeToLive, timeToLive); // Enable Multicast Loopback sock.SetSocketOption(sOL, SocketOptionName.MulticastLoopback, 1); } else { // Enable Unicast Loopback echoEndPoint = new IPEndPoint(externalInterface, endPoint.Port); } }
/// <summary> /// Constructor that binds this object instance to an IPEndPoint. If you need to change IPEndPoint dynamically, Dispose and recreate a new object. /// </summary> /// <param name="endPoint">IPEndPoint where we should be listening for IP Multicast packets.</param> /// <param name="TimeoutMilliseconds">Milliseconds before lack of a packet == a Network Timeout</param> /// <example> /// ... /// MulticastUdpListener mcListener = new MulticastUdpListener(endPoint1); /// mcListener.Receive(packetBuffer); /// mcListener.Displose(); /// /// MulticastUdpListener mcListener = new MulticastUdpListener(endPoint2); /// mcListener.Receive(packetBuffer); /// mcListener.Displose(); /// /// mcListener = null; /// ... /// </example> public UdpListener(System.Net.IPEndPoint endPoint, int timeoutMilliseconds) { LstSocks.Socket.SockInterfacePair sip = LstSocks.Socket.GetSharedSocket(endPoint); this.sock = sip.sock; this.externalInterface = sip.extInterface; this.ep = endPoint; lock(sip) { if(!sip.Initialized) { try { // Set the timeout on the socket if( timeoutMilliseconds > 0 ) sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeoutMilliseconds); // Set the socket to send & receive from this endpoint sock.Bind(new IPEndPoint(externalInterface,endPoint.Port)); // Make room for 80 packets plus some overhead sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 1500 * 80); if(Utility.IsMulticast(endPoint.Address)) { if(endPoint.AddressFamily == AddressFamily.InterNetworkV6) { // Join the IPv6 Multicast group sock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(endPoint.Address)); } else { // Join the IPv4 Multicast group sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(endPoint.Address)); } } sip.Initialized = true; } catch { this.Dispose(); throw; } } } }