TCP服务器客户端问题

时间:2010-09-30 13:15:45

标签: c# sockets tcp

首先我在套接字编程中是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数据丢失。我应该如何改进我的代码?大文件问题并不那么重要,主要问题是快速数据发送中的数据丢失。 tnx寻求帮助

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

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

}

但问题仍然存在

编辑 - &GT;大文件问题修复它只是一个'System.OutOfMemoryException',但它没有抛出错误。

2 个答案:

答案 0 :(得分:0)

  • 您应该将所有字节数组更改为List<byte>。你现在正在疯狂地创建实例。 GC可能比它需要的工作量大得多。这可能会减慢它的速度,以至于它无法跟上。

与套接字没有关系:

  • 将尺码设为const
  • 永远不要锁定this。创建一个可以锁定的私有字段。事实上,我甚至认为你不需要锁定。
  • 删除错误字符串。

答案 1 :(得分:0)

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

我的优化代码是

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毫秒,没有数据丢失:) 谢谢你的帮助

相关问题