当我在客户端上捕获超时的SocketException时,服务器上会发生什么?

时间:2016-12-15 11:45:23

标签: c# sql-server sockets tcplistener

我正在使用.NET Framework 4.6.2和C#开发套接字服务器和客户端。

在服务器上,我在数据库中执行操作(使用存储过程更新表)。

在客户端,我发送数据以更新数据库。

由于TimeOut,我在客户端上获得套接字异常之前一切正常。之后,我在数据库更新时遇到错误。存储过程不会失败,但它不起作用。换句话说,它返回0,因为我已检查存储过程该行已更新但该行尚未更新:

override func contents(forType typeName: String) throws -> Any {  
    guard let data = text.data(using: .utf8) else { ... }
    if #available(iOS 10, *) {
        return data
    } else {
        return NSMutableData(data: data)
    }
}

此处,存储过程返回0,但如果我在SSMS上执行set @commFlag = (Select CommissioningFlag from Code where CodeId = @codeId); if (@commFlag = @newCommFlagValue) begin return 0; end else return -1; ,则返回先前的Select CommissioningFlag from Code where CodeId = @codeId值。

我已经解决了这个问题,当我收到Timeout SocketException时,在客户端执行重置:

CommissioningFlag

private SynchronousSocketClient trzCodeReceiverClient; [ ... ] try { trzCodeReceiverClient.SendData(dataFrame, out dataReceived); } catch (SocketException ex) { dataReceived = Resources.ErrorSocketTrzCodeReceiver; RestartSocket(); } catch (Exception ex) { dataReceived = Resources.ErrorSocketTrzCodeReceiver; RestartSocket(); } 方法:

RestartSocket

private void RestartSocket() { if (trzCodeReceiverClient != null) trzCodeReceiverClient.EndConnection(); NameValueCollection section = ConfigurationManager.GetSection("TRZCodeReceiverSection") as NameValueCollection; string trzCodeReceiverHost = section["host"]; int trzCodeReceiverPort = int.Parse(section["port"]); int receiveTimeOut = int.Parse(section["receiveTimeOut"]); trzCodeReceiverClient = new SynchronousSocketClient( trzCodeReceiverHost, trzCodeReceiverPort, receiveTimeOut); trzCodeReceiverClient.EstablishConnection(); } 就是class

但我认为这不是正确的做法。我不想做SynchronousSocketClient。也许服务器在Timeout之后处于不稳定状态,或者我不知道。

你知道这里发生了什么吗?

我认为问题可能是我不等待从服务器接收响应,因为我抛出了RestartSocket

服务器类或多或少类似于one

0 个答案:

没有答案
相关问题