简体   繁体   English

TCP服务器客户端问题

[英]TCP server client issue

First i am n00b in socket programming. 首先我在套接字编程中是n00b。 So i decided to write simple data over lan tcp server My server code that handles incomming data is 因此,我决定在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;
}

it works but have some issues. 它有效,但存在一些问题。 It fales receiving files bigger then 100 mbs or smthng and also if i send data very often interval < 800 then data is lost. 接收大于100 mbs或smthng的文件会失败,而且如果我经常发送数据间隔<800,则会丢失数据。 how should i improve my code? 我应该如何改善我的代码? The large file issue is not so important the primary issue is the data loss in fast data sending. 大文件问题并不是很重要,主要问题是快速数据发送中的数据丢失。 tnx for help 寻求帮助

Ok i now updated the code by the suggestions 好的,我现在通过建议更新了代码

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();

}

but the issues still there 但是问题仍然存在

Edit--> large file issue fixed it was just a 'System.OutOfMemoryException' but it didn't throw a error. 编辑->大文件问题已修复,它只是一个“ System.OutOfMemoryException”,但没有引发错误。

  • The All byte array you should change to a List<byte> . 您应该将All字节数组更改为List<byte> You are creating instances like crazy right now. 您现在正在创建疯狂的实例。 The GC is probably working a lot more than it needs to. GC的工作量可能远远超过了需要。 This might be slowing it down so much that it can't keep up. 这可能会减慢速度,以至于无法跟上。

Not really related to sockets: 与套接字没有真正的关系:

  • Make size a const 将大小设为const
  • NEVER lock this . 永远不要锁定this Create a private field that you can lock. 创建一个可以锁定的私有字段。 In fact, I don't even think you need a lock here. 实际上,我什至都不认为您需要在这里锁。
  • remove the error string. 删除错误字符串。

OK i solved the problem. 好吧,我解决了这个问题。 I simple send to much data to fast so data loss was unavoidable. 我简单地快速发送大量数据,所以数据丢失是不可避免的。

My optimized code is 我优化的代码是

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);
    }
}

i have tested intervals as low as 1 ms and no data loss :) thanks for your help 我测试的间隔低至1毫秒,并且没有数据丢失:)谢谢您的帮助

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

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