Example #1
0
        public unsafe override IPEndPoint[] GetActiveUdpListeners()
        {
            int realCount = Interop.Sys.GetEstimatedUdpListenerCount();
            int infoCount = (int)(realCount * 1.5f);

            Interop.Sys.IPEndPointInfo *infos = stackalloc Interop.Sys.IPEndPointInfo[infoCount];
            while (Interop.Sys.GetActiveUdpListeners(infos, &infoCount) == -1)
            {
                var newAlloc = stackalloc Interop.Sys.IPEndPointInfo[infoCount];
                infos = newAlloc;
            }

            IPEndPoint[] endPoints = new IPEndPoint[infoCount];
            for (int i = 0; i < infoCount; i++)
            {
                Interop.Sys.IPEndPointInfo endPointInfo = infos[i];
                int       port = (int)endPointInfo.Port;
                IPAddress ipAddress;
                if (endPointInfo.NumAddressBytes == 0)
                {
                    ipAddress = IPAddress.Any;
                }
                else
                {
                    byte[] bytes = new byte[endPointInfo.NumAddressBytes];
                    fixed(byte *bytesPtr = bytes)
                    {
                        Buffer.MemoryCopy(endPointInfo.AddressBytes, bytesPtr, bytes.Length, bytes.Length);
                    }

                    ipAddress = new IPAddress(bytes);
                }

                endPoints[i] = new IPEndPoint(ipAddress, port);
            }

            return(endPoints);
        }
Example #2
0
        public unsafe override IPEndPoint[] GetActiveUdpListeners()
        {
            int realCount = Interop.Sys.GetEstimatedUdpListenerCount();
            int infoCount = realCount * 2;

            Interop.Sys.IPEndPointInfo *infos = stackalloc Interop.Sys.IPEndPointInfo[infoCount];
            if (Interop.Sys.GetActiveUdpListeners(infos, &infoCount) == -1)
            {
                throw new NetworkInformationException(SR.net_PInvokeError);
            }

            IPEndPoint[] endPoints = new IPEndPoint[infoCount];
            for (int i = 0; i < infoCount; i++)
            {
                Interop.Sys.IPEndPointInfo endPointInfo = infos[i];
                int       port = (int)endPointInfo.Port;
                IPAddress ipAddress;
                if (endPointInfo.NumAddressBytes == 0)
                {
                    ipAddress = IPAddress.Any;
                }
                else
                {
                    byte[] bytes = new byte[endPointInfo.NumAddressBytes];
                    fixed(byte *bytesPtr = &bytes[0])
                    {
                        Buffer.MemoryCopy(endPointInfo.AddressBytes, bytesPtr, bytes.Length, bytes.Length);
                    }

                    ipAddress = new IPAddress(bytes);
                }

                endPoints[i] = new IPEndPoint(ipAddress, port);
            }

            return(endPoints);
        }