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


C# Socket.EndSendTo方法代码示例

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


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

示例1: DiscoverAsync

	    public static async Task<bool> DiscoverAsync(object state = null)
	    {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
            IPEndPoint endpoint = new IPEndPoint(IPAddress.Broadcast, 1900);

            byte[] outBuf = Encoding.ASCII.GetBytes(BroadcastPacket);
            byte[] inBuf = new byte[4096];

	        int tries = MaxTries;
	        while (--tries > 0)
	        {
	            try
	            {
	                TaskFactory factory = new TaskFactory();
	                sock.ReceiveTimeout = DiscoverTimeout;
	                await factory.FromAsync(sock.BeginSendTo(outBuf, 0, outBuf.Length, 0, endpoint, null, null), end =>
	                {
	                    sock.EndSendTo(end);
	                }).ConfigureAwait(false);
	                var ar = sock.BeginReceive(inBuf, 0, inBuf.Length, 0, null, null);
	                if (ar == null) throw new Exception("ar is null");
                    int length = await factory.FromAsync(ar, end => sock.EndReceive(end)).ConfigureAwait(false);

                    string data = Encoding.ASCII.GetString(inBuf, 0, length).ToLowerInvariant();

                    var match = ResponseLocation.Match(data);
                    if (match.Success && match.Groups["location"].Success)
                    {
                        System.Diagnostics.Debug.WriteLine("Found UPnP device at " + match.Groups["location"]);
                        string controlUrl = GetServiceUrl(match.Groups["location"].Value);
                        if (!string.IsNullOrEmpty(controlUrl))
                        {
                            _controlUrl = controlUrl;
                            System.Diagnostics.Debug.WriteLine("Found control URL at " + _controlUrl);
                            return true;                            
                        }
                    }

                }
	            catch (Exception ex)
	            {
	                // ignore timeout exceptions
	                if (!(ex is SocketException && ((SocketException) ex).ErrorCode == 10060))
	                {
	                    System.Diagnostics.Debug.WriteLine(ex.ToString());
	                }
	            }
	        }
            return false;
	    }
开发者ID:pendo324,项目名称:Cobalt,代码行数:51,代码来源:NatHelper.cs

示例2: SendAsync

        /// <summary>
        /// Sends an <see cref="ISnmpMessage"/>.
        /// </summary>
        /// <param name="message">The <see cref="ISnmpMessage"/>.</param>
        /// <param name="manager">Manager</param>
        /// <param name="socket">The socket.</param>
        public static void SendAsync(this ISnmpMessage message, EndPoint manager, Socket socket)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }

            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }

            var code = message.TypeCode();
            if ((code != SnmpType.TrapV1Pdu && code != SnmpType.TrapV2Pdu) && code != SnmpType.ReportPdu)
            {
                throw new InvalidOperationException(string.Format(
                    CultureInfo.InvariantCulture,
                    "not a trap message: {0}",
                    code));
            }

            var bytes = message.ToBytes();
            socket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, manager, ar => socket.EndSendTo(ar), null);
        }
开发者ID:nickolaykon,项目名称:sharpsnmplib,代码行数:35,代码来源:SnmpMessageExtension.cs


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