當前位置: 首頁>>代碼示例>>C#>>正文


C# AddressFamily.ToString方法代碼示例

本文整理匯總了C#中AddressFamily.ToString方法的典型用法代碼示例。如果您正苦於以下問題:C# AddressFamily.ToString方法的具體用法?C# AddressFamily.ToString怎麽用?C# AddressFamily.ToString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在AddressFamily的用法示例。


在下文中一共展示了AddressFamily.ToString方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: IsProtocolSupported

        private static bool IsProtocolSupported(AddressFamily af)
        {
            int family;
            switch (af)
            {
                case AddressFamily.InterNetwork:
                    family = Interop.libc.AF_INET;
                    break;
                case AddressFamily.InterNetworkV6:
                    family = Interop.libc.AF_INET6;
                    break;
                default:
                    Debug.Fail("Invalid address family: " + af.ToString());
                    throw new ArgumentException("af");
            }

            int socket = -1;
            try
            {
                socket = Interop.libc.socket(family, Interop.libc.SOCK_DGRAM, 0);
                if (socket == -1)
                {
                    return Interop.Sys.GetLastError() != Interop.Error.EAFNOSUPPORT;
                }
                return true;
            }
            finally
            {
                if (socket != -1)
                {
                    Interop.Sys.Close(socket);
                }
            }
        }
開發者ID:nblumhardt,項目名稱:corefx,代碼行數:34,代碼來源:SocketProtocolSupportPal.Unix.cs

示例2: IsProtocolSupported

        private static bool IsProtocolSupported(AddressFamily af)
        {
            SocketError errorCode;
            IntPtr s = IntPtr.Zero;
            bool ret = true;

            try
            {
                s = Interop.Winsock.WSASocketW(af, SocketType.Dgram, 0, IntPtr.Zero, 0, 0);

                if (s == IntPtr.Zero)
                {
                    errorCode = (SocketError)Marshal.GetLastWin32Error();
                    if (errorCode == SocketError.AddressFamilyNotSupported)
                    {
                        ret = false;
                    }
                }
            }
            finally
            {
                if (s != IntPtr.Zero)
                {
                    SocketError closeResult = Interop.Winsock.closesocket(s);
#if DEBUG
                    if (closeResult != SocketError.Success)
                    {
                        errorCode = (SocketError)Marshal.GetLastWin32Error();
                        Debug.Fail("Failed to detect " + af.ToString() + " protocol: " + errorCode.ToString());
                    }
#endif
                }
            }

            return ret;
        }
開發者ID:nblumhardt,項目名稱:corefx,代碼行數:36,代碼來源:SocketProtocolSupportPal.Windows.cs


注:本文中的AddressFamily.ToString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。