繁体   English   中英

TCP服务器客户端问题

[英]TCP server client issue

首先我在套接字编程中是n00b。 因此,我决定在lan tcp服务器上编写简单的数据。处理传入数据的服务器代码是

    private void HandleClientComm(object client)
{

    TcpClient tcpClient = (TcpClient)client;
    NetworkStream clientStream = tcpClient.GetStream();
    clientStream.ReadTimeout = 10;
    int size = 4096 * 1000;
    byte[] message = new byte[size];
    byte[] All = new byte[0];
    int bytesRead;
    string error = "";
    lock (this)
    {
        while (true)
        {
            All = new byte[0];
            while (true)
            {
                bytesRead = 0;
                try
                {
                    bytesRead = clientStream.Read(message, 0, size);
                    All = AddBArrays(All, message, bytesRead);
                }
                catch
                {
                    break;
                }

                if (bytesRead == 0)
                {
                    break;
                }

            }
            if (All.Length > 0)
            {
                Message m = (Message)Tools.ByteArrayToObject(All);
                OnRecived(new RecivedArgs("localhost", (Message)Tools.ByteArrayToObject(All)));
            }
        }
        tcpClient.Close();
    }
}
byte[] AddBArrays(byte[] ar1, byte[] ar2, int read)
{
    byte[] concat = new byte[ar1.Length + read];
    if (ar1.Length != 0)
        System.Buffer.BlockCopy(ar1, 0, concat, 0, ar1.Length);
    System.Buffer.BlockCopy(ar2, 0, concat, ar1.Length, read);
    return concat;
}

它有效,但存在一些问题。 接收大于100 mbs或smthng的文件会失败,而且如果我经常发送数据间隔<800,则会丢失数据。 我应该如何改善我的代码? 大文件问题并不是很重要,主要问题是快速数据发送中的数据丢失。 寻求帮助

好的,我现在通过建议更新了代码

private void HandleClientComm(object client)
{

    TcpClient tcpClient = (TcpClient)client;
    NetworkStream clientStream = tcpClient.GetStream();
    clientStream.ReadTimeout = 10;
    int size = 4096 * 1000;
    List<byte> Test = new List<byte>();
    byte[] message = new byte[size];
    byte[] All = new byte[0];
    int bytesRead;
    while (true)
    {
        //All = new byte[0];
        while (true)
        {
            bytesRead = 0;
            try
            {
                bytesRead = clientStream.Read(message, 0, size);
                for (int i = 0; i < bytesRead; i++)
                {
                    Test.Add(message[i]);
                }
            }
            catch
            {
                break;
            }

            if (bytesRead == 0)
            {
                break;
            }
        }
        if (Test.Count > 0)
        {
            Message m = (Message)Tools.ByteArrayToObject(Test.ToArray());
            OnRecived(new RecivedArgs("localhost", m));
            Test = new List<byte>();
        }
    }
    tcpClient.Close();

}

但是问题仍然存在

编辑->大文件问题已修复,它只是一个“ System.OutOfMemoryException”,但没有引发错误。

  • 您应该将All字节数组更改为List<byte> 您现在正在创建疯狂的实例。 GC的工作量可能远远超过了需要。 这可能会减慢速度,以至于无法跟上。

与套接字没有真正的关系:

  • 将大小设为const
  • 永远不要锁定this 创建一个可以锁定的私有字段。 实际上,我什至都不认为您需要在这里锁。
  • 删除错误字符串。

好吧,我解决了这个问题。 我简单地快速发送大量数据,所以数据丢失是不可避免的。

我优化的代码是

private void HandleClientComm(object client)
{
    TcpClient tcpClient = (TcpClient)client;
    NetworkStream clientStream = tcpClient.GetStream();
    int size = 1;
    byte[] message = new byte[1];
    int bytesRead;
    while (true)
    {
        bytesRead = 0;
        if (clientStream.DataAvailable)
            bytesRead = clientStream.Read(message, 0, 1);
        if (bytesRead > 0)
        {
            OnRecived(new RecivedArgs("tick", null));
        }
        Thread.Sleep(1);
    }
}

我测试的间隔低至1毫秒,并且没有数据丢失:)谢谢您的帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM