套接字连接无法发送数据

时间:2017-11-06 17:00:12

标签: c# sockets tcp tcpserver

我有一个长时间运行的TCP连接。一台机器(物联网设备)与服务器建立连接,建立连接(加密和填充)并存储数据,连接保持打开状态。

一切都有效但有时服务器“丢弃”连接错误: 已建立的连接被主机中的软件中止(代码:10053 - ConnectionAborted)。但是连接没有被删除,导致服务器在错误发生后可以从设备读取数据并且可以开始发送再次。如果连接实际丢失,服务器和客户端都需要重新初始化连接(安全性和东西)。

没有什么能够说明无法写入网络流的原因。 并且轮询套接字说,它是可写的,并且在下一点它会引发异常。似乎是随机发生的。

public class ClientIdentifier
{
    ...
    public TcpClient Connection { get; set; }
    public BlockDecoder ConnectionDecoder { get; set; }
}

private void ReplyToClient(ClientIdentifier client, byte[] data)
{
    byte[] encrypted = client.ConnectionDecoder.Encrypt(data);
    var stream = client.Connection.GetStream();

    int dataIndex = 0;

    while (dataIndex != encrypted.Length)
    {
        if (CanWriteClient(client))
        {
            byte[] block = encrypted.GetChunk(dataIndex, Frame.BLOCK_LENGTH);

            stream.Write(block, 0, Frame.BLOCK_LENGTH);

            dataIndex += Frame.BLOCK_LENGTH;
        }
    }
}

private bool CanWriteClient(ClientIdentifier client)
{
    try
    {
        return client.Connection.Client.Poll(1000, SelectMode.SelectWrite);
    }
    catch (Exception ex)
    {
        Logger.Warn(ex, $"[{client.HexIdentifier}]: Polling client write failed");
        return false;
    }
}

编辑(12.11.2017)

服务器:192.168.1.150 装置:192.168.1.201

我可以看到当服务器以某种奇怪的方式重置Seq和Ack时设备发送RST。

Device Error

1 个答案:

答案 0 :(得分:0)

管理找到错误。超时设置得太低了。

newtype NodesState = NodesState {
    nodes  :: Array Node ,
    errors :: Array String 
}

nodes_state :: NodesState 
nodes_state = NodesState { nodes: [], errors: [] }

nodesList :: forall props. ReactClass props
nodesList = createClass $ spec nodes_state \ctx -> do 
    NodesState { nodes: nodes, errors: errors } <- readState ctx
相关问题