/// <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> /// Common initialization for unicast (reflector) and multicast sockets /// </summary> /// <param name="port"> The local port number on which packets will be received. This can be /// zero to indicate a "don't care" port should be selected. /// </param> /// <param name="timeoutMilliseconds"></param> private void InitializeSocket(int port, int timeoutMilliseconds) { // 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, port)); // Make room for 80 packets plus some overhead sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, 1500 * 80); }
/// <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; } } } }