当前位置: 首页>>代码示例>>C#>>正文


C# Socket.GetSocketOption方法代码示例

本文整理汇总了C#中Socket.GetSocketOption方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.GetSocketOption方法的具体用法?C# Socket.GetSocketOption怎么用?C# Socket.GetSocketOption使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Socket的用法示例。


在下文中一共展示了Socket.GetSocketOption方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReuseUnicastPort_CreateSocketGetOption_NoSocketsReuseUnicastPortSupport_Throws

 public void ReuseUnicastPort_CreateSocketGetOption_NoSocketsReuseUnicastPortSupport_Throws()
 {
     var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     {
         Assert.Throws<SocketException>(() =>
             socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort));
     }
     else
     {
         Assert.Throws<PlatformNotSupportedException>(() =>
             socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort));
     }
 }
开发者ID:noahfalk,项目名称:corefx,代码行数:15,代码来源:SocketOptionNameTest.cs

示例2: ReuseUnicastPort_CreateSocketGetOption_SocketsReuseUnicastPortSupport_OptionIsZero

 public void ReuseUnicastPort_CreateSocketGetOption_SocketsReuseUnicastPortSupport_OptionIsZero()
 {
     var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     
     var optionValue = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort);
     Assert.Equal(0, optionValue);
 }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:7,代码来源:SocketOptionNameTest.cs

示例3: ReuseUnicastPort_CreateSocketGetOption_NoSocketsReuseUnicastPortSupport_Throws

        public void ReuseUnicastPort_CreateSocketGetOption_NoSocketsReuseUnicastPortSupport_Throws()
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            Assert.Throws<SocketException>(() =>
                socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseUnicastPort));
        }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:7,代码来源:SocketOptionNameTest.cs

示例4: Main

    public static void Main()
    {
        string input, stringData;
          int recv;
          IPEndPoint ipep = new IPEndPoint(
                      IPAddress.Parse("127.0.0.1"), 9050);

          Socket server = new Socket(AddressFamily.InterNetwork,
                     SocketType.Dgram, ProtocolType.Udp);

          int sockopt = (int)server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
          Console.WriteLine("Default timeout: {0}", sockopt);
          server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
          sockopt = (int)server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
          Console.WriteLine("New timeout: {0}", sockopt);

          string welcome = "Hello, are you there?";
          data = Encoding.ASCII.GetBytes(welcome);

          recv = AdvSndRcvData(server, data, ipep);
          if (recv > 0)
          {
         stringData = Encoding.ASCII.GetString(data, 0, recv);
         Console.WriteLine(stringData);
          } else
          {
         Console.WriteLine("Unable to communicate with remote host");
         return;
          }

          while(true)
          {
         input = Console.ReadLine();
         if (input == "exit")
            break;
         recv = AdvSndRcvData(server, Encoding.ASCII.GetBytes(input), ipep);
         if (recv > 0)
         {
            stringData = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine(stringData);
         } else
            Console.WriteLine("Did not receive an answer");
          }
          Console.WriteLine("Stopping client");
          server.Close();
    }
开发者ID:NourGalaby,项目名称:UDP_PROJECT,代码行数:46,代码来源:asd.cs

示例5: MulticastOption_CreateSocketSetGetOption_GroupAndInterfaceIndex_SetSucceeds_GetThrows

        public void MulticastOption_CreateSocketSetGetOption_GroupAndInterfaceIndex_SetSucceeds_GetThrows()
        {
            int interfaceIndex = 0;
            IPAddress groupIp = IPAddress.Parse("239.1.2.3");

            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
            {
                socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(groupIp, interfaceIndex));

                Assert.Throws<SocketException>(() => socket.GetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership));
            }
        }
开发者ID:swaroop-sridhar,项目名称:corefx,代码行数:12,代码来源:SocketOptionNameTest.cs

示例6: FailedConnect_GetSocketOption_SocketOptionNameError

        public void FailedConnect_GetSocketOption_SocketOptionNameError(bool simpleGet)
        {
            using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) { Blocking = false })
            {
                // Fail a Connect
                using (var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    server.Bind(new IPEndPoint(IPAddress.Loopback, 0)); // bind but don't listen
                    Assert.ThrowsAny<Exception>(() => client.Connect(server.LocalEndPoint));
                }

                // Verify via Select that there's an error
                const int FailedTimeout = 10 * 1000 * 1000; // 10 seconds
                var errorList = new List<Socket> { client };
                Socket.Select(null, null, errorList, FailedTimeout);
                Assert.Equal(1, errorList.Count);

                // Get the last error and validate it's what's expected
                int errorCode;
                if (simpleGet)
                {
                    errorCode = (int)client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);
                }
                else
                {
                    byte[] optionValue = new byte[sizeof(int)];
                    client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error, optionValue);
                    errorCode = BitConverter.ToInt32(optionValue, 0);
                }
                Assert.Equal((int)SocketError.ConnectionRefused, errorCode);

                // Then get it again
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // The Windows implementation doesn't clear the error code after retrieved.
                    // https://github.com/dotnet/corefx/issues/8464
                    Assert.Equal(errorCode, (int)client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error));
                }
                else
                {
                    // The Unix implementation matches the getsockopt and MSDN docs and clears the error code as part of retrieval.
                    Assert.Equal((int)SocketError.Success, (int)client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error));
                }
            }
        }
开发者ID:swaroop-sridhar,项目名称:corefx,代码行数:45,代码来源:SocketOptionNameTest.cs

示例7: SetIPProtectionLevel_Windows

        public void SetIPProtectionLevel_Windows(IPProtectionLevel level, AddressFamily family, SocketOptionLevel optionLevel)
        {
            using (var socket = new Socket(family, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.SetIPProtectionLevel(level);

                int result = (int)socket.GetSocketOption(optionLevel, SocketOptionName.IPProtectionLevel);
                Assert.Equal(result, (int)level);
            }
        }
开发者ID:dotnet,项目名称:corefx,代码行数:10,代码来源:SocketOptionNameTest.cs


注:本文中的Socket.GetSocketOption方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。