Stream Socket错误处理winRT(Stream Socket error handling winRT)
我正在使用流套接字,
根据msdn文档:
处理异常
在StreamSocket类上调用异步方法时,必须编写代码来处理异常。 参数验证错误,名称解析失败和网络错误可能导致异常。 网络错误(例如,连接丢失,连接失败和服务器故障)的例外情况可能随时发生。 这些错误导致抛出异常。 如果您的应用未处理,则异常可能导致整个应用程序被运行时终止。
Windows.Networking.Sockets命名空间具有在使用套接字时简化处理错误的功能。 SocketError类上的GetStatus方法可以将HRESULT从异常转换为SocketErrorStatus枚举值。 这对于在应用中以不同方式处理特定网络异常非常有用。 应用程序还可以使用来自参数验证错误的异常的HRESULT来了解有关导致异常的错误的更多详细信息。
所以我使用以下代码来处理套接字连接错误状态。
try { var socket = new StreamSocket(); HostName host = new HostName("www.google.com"); // connection is executed synchronously socket.ConnectAsync(host, "2000", SocketProtectionLevel.PlainSocket).AsTask().Wait(); Debug.WriteLine("Success"); } catch (Exception ex) { SocketErrorStatus socketErrorStatus = SocketError.GetStatus(ex.HResult); switch(socketErrorStatus) { case SocketErrorStatus.ConnectionTimedOut: //do something break; case SocketErrorStatus.HostNotFound: //do something break; default: break; } }
但是套接字错误返回的异常对象不包含有效的HResult。
以下是结果异常对象:
Count =当前上下文中不存在名称'InnerExceptionCount'[System.AggregateException]:Count =当前上下文中不存在名称'InnerExceptionCount'
数据 :{System.Collections.ListDictionaryInternal}
HelpLink :null
HResult :-2146233088
InnerException: {System.Exception:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应。 (HRESULT异常:0x8007274C)}
消息: “发生了一个或多个错误。”
资料来源 :“mscorlib”
StackTrace :“System.Threading.Tasks.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)\ r \ n在System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout,CancellationToken cancellationToken)\ r \ n在System.Threading.Tasks.Task .Wait()\ r \ n在StreamSokcetSample.MainPage.Button_Tapped(Object sender,TappedRoutedEventArgs e)“在这种情况下,我总是得到SocketErrorStatus.Unknown(默认值)作为结果,而当我将HRESULT的值:0x8007274C传递给GetStatus时 ,它会产生正确的输出(ConnectionTimedOut = 3)。
InnerException: {System.Exception:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应。 ( HRESULT异常:0x8007274C )}
我可以依赖内部异常消息并从那里获取HRESULT吗?
有没有其他方法可以获得理想的结果?
I am working on stream socket,
According to msdn documentaion:
Handling exceptions
You must write code to handle exceptions when you call asynchronous methods on the StreamSocket class. Exceptions can result from parameter validation errors, name resolutions failures, and network errors. Exceptions from network errors (loss of connectivity, connection failures, and server failures, for example) can happen at any time. These errors result in exceptions being thrown. If not handled by your app, an exception can cause your entire app to be terminated by the runtime.
The Windows.Networking.Sockets namespace has features that simplify handling errors when using sockets. The GetStatus method on the SocketError class can convert the HRESULT from an exception to a SocketErrorStatus enumeration value. This can be useful for handling specific network exceptions differently in your app. An app can also use the HRESULT from the exception on parameter validation errors to learn more detailed information on the error that caused the exception.
So I have used following code to handle socket connect error states.
try { var socket = new StreamSocket(); HostName host = new HostName("www.google.com"); // connection is executed synchronously socket.ConnectAsync(host, "2000", SocketProtectionLevel.PlainSocket).AsTask().Wait(); Debug.WriteLine("Success"); } catch (Exception ex) { SocketErrorStatus socketErrorStatus = SocketError.GetStatus(ex.HResult); switch(socketErrorStatus) { case SocketErrorStatus.ConnectionTimedOut: //do something break; case SocketErrorStatus.HostNotFound: //do something break; default: break; } }
But the exception object returned on socket error doesn't contain valid HResult.
Following is resultant exception object:
Count = The name 'InnerExceptionCount' does not exist in the current context [System.AggregateException]: Count = The name 'InnerExceptionCount' does not exist in the current context
Data: {System.Collections.ListDictionaryInternal}
HelpLink: null
HResult: -2146233088
InnerException: {System.Exception: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (Exception from HRESULT: 0x8007274C)}
Message: "One or more errors occurred."
Source: "mscorlib"
StackTrace: " at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)\r\n at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)\r\n at System.Threading.Tasks.Task.Wait()\r\n at StreamSokcetSample.MainPage.Button_Tapped(Object sender, TappedRoutedEventArgs e)"In this situation I am always getting SocketErrorStatus.Unknown(default value) as result whereas when I pass int value of HRESULT: 0x8007274C to GetStatus, it results in correct output(ConnectionTimedOut = 3).
InnerException: {System.Exception: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (Exception from HRESULT: 0x8007274C)}
Can I rely upon inner exception message and fetch HRESULT from there?
Is there any other way to get desired results?
原文:https://stackoverflow.com/questions/31513254
最满意答案
我猜你想在这里
knife node edit
,而不是knife edit node
。 在这种情况下,您可以在chef-repo的任何地方。I'm guessing you want
knife node edit
here, rather thanknife edit node
. In that case, you can be anywhere in the chef-repo.
相关问答
更多-
你在问节点编辑表单吗? 如果是这样,您可以执行以下操作: if (empty($form['nid']['#value'])) { // in add form } else { // in edit form } 要么... if (arg(1) == "add") { // in add form... } else { // in edit form... } Are you asking for the node edit form? If so, you can either ...
-
刀编辑节点(Knife edit node)[2022-12-02]
我猜你想在这里knife node edit ,而不是knife edit node 。 在这种情况下,您可以在chef-repo的任何地方。 I'm guessing you want knife node edit here, rather than knife edit node. In that case, you can be anywhere in the chef-repo. -
该事件处理程序应该应用于外部窗口,而不是treeView $("") .html(editTemplate({ node: node})) .appendTo("body") .kendoWindow({ modal: true, //visible: true, deactivate: function () { this.destroy(); } }) .on("clic ...
-
没有Node.getTextContents()的Java XML节点编辑(Java XML Node Edit without Node.getTextContents())[2022-04-22]
该文本是MyEle元素的子节点,因此您可以使用以下内容: MyEle.getFirstChild().getNodeValue() The text is a child node of the MyEle element, so you would use something like: MyEle.getFirstChild().getNodeValue() -
在EcoResCategory类的initializeHelper方法中,有一个读取的初始化行 EcoResCategoryLookupParameters lookupParameters = new EcoResCategoryLookupParameters(false, false, false); 第二个布尔参数是boolean _showFriendlyNameWithName (此处有文档: http : //msdn.microsoft.com/en-us/library/ecoresca ...
-
编辑Xml节点(Edit Xml Node)[2021-09-12]
XmlDocument doc = new XmlDocument(); doc.Load(@"Test.xml"); XmlNodeList elem = doc.GetElementsByTagName("Data"); foreach (XmlNode tag in elem) { //do whatever you want to the attribute using SetAttribute method } XmlElement.GetElementsByTagName方法可以做到这一点 ... -
您可能会使用某种形式的knife exec脚本来执行此操作,但是如果没有更多信息,很难确切地知道要使用哪些代码。 You would probably do this with a knife exec script of some form, but it's hard to know exactly what code to use without more information.
-
这在结构上是不可能的,除了正常之外的所有级别都是从头开始重建,以防止散乱。 This is structurally impossible, all levels other than normal are rebuilt from scratch every converge to prevent stragglers.
-
您可以询问TTreeView.IsEditing属性。 从参考: 指示用户当前是否正在编辑节点。 如果正在编辑树视图中的任何节点标签,则IsEditing返回true。 You can ask the TTreeView.IsEditing property. From the reference: Indicates whether a node is currently being edited by the user. IsEditing returns true if any node label ...
-
您可以使用Element.classList及其remove()方法 AB.classList.remove('x-form-trigger').remove('x-form-trigger-default') 或者,使用Element.className AB.className = "button-cross button-cross-default" You can use Element.classList and its remove() method AB.classList.remove ...