当前位置: 首页 > 知识库问答 >
问题:

为什么我的TCP应用程序在快速发送时会错过一些数据包

杜海
2023-03-14

我正在使用异步套接字用C#编写一个多客户端/服务器应用程序。连接上的每个客户端发送10个项目。问题是,当快速启动大量客户机时,每个客户机发送的条目都少于10个,有时什么也不发送,服务器只记录它们的连接。

private void Recieve(IAsyncResult iar) //Called when socket receives something.
{
    Socket server_conn = (Socket)iar.AsyncState;

    if (!SocketConnected(server_conn))
    {
        server_conn.Close();
        logthis("Client Disconnected");
        return;
    }
    int n = server_conn.EndReceive(iar);  //Stop Receiving and parse data, n is number of bytes received

    ClientData asdf = null;
    foreach (ClientData cls in clientlist)
    {
        if (server_conn.RemoteEndPoint == cls.clientsock.RemoteEndPoint) //Who sent this data
        {
            asdf = cls; //cls is who sent this data

            //Start a new thread and pass received bytes to it in order to be parsed
            var t = new Thread(() => parse(cls, n,cls.recvbuffer));
            t.Start();
            Thread.Sleep(100);
            break;
        }
    }

    asdf.recvbuffer = new byte[1024];      //Clear buffer of client
    server_conn.BeginReceive(asdf.recvbuffer, 0, asdf.recvbuffer.Length, SocketFlags.None, new AsyncCallback(Recieve), server_conn); //Start receiving again

}

private void parse(ClientData theclient, int nobytesreceived, byte[] bytesreceived)
{
    ClientData cls = theclient;
    int n = nobytesreceived;
    byte[] receivedbytes = bytesreceived;


    lock(s)
    {
    if (!cls.dataphase)  //If there's no fragmented packets still waiting to be read
    {
        cls.dataphase = true;   
        byte[] sizeinbytes = new byte[4];
        for (int i = 0; i < 4; i++)
        {
            sizeinbytes[i] = receivedbytes[i];
        }
        int size = BitConverter.ToInt32(sizeinbytes, 0);  //Read first four bytes of packet to get size of data

        if ((n - 4) == size)  //If received number of bytes - 4 is equals to datasize
        {
            byte[] payload = new byte[size];
            Array.Copy(receivedbytes, 4, payload, 0, (n - 4)); //Copy data to separately to payload array to be displayed to user
            logthis(cls.clientsock.RemoteEndPoint.ToString());
            logthis(Encoding.ASCII.GetString(payload));
            cls.dataphase = false;       //packet read successfully 
        }
        else if ((n - 4) < size) //If received amount of bytes is less than data size (fragmented data)
        {
            cls.data = new byte[size];
            for (int i = 4; i <= n - 4; i++)
            {
                cls.data[i - 4] += receivedbytes[i];
            }

            cls.datasize = size; //And cls.dataphase will remain true so it can be handled correctly the next time we receive something from same client
        }
        else if((n-4) > size)  //If received amount of bytes is bigger than data (lumped packets)
        {
            byte[] payload = new byte[size];
            byte[] otherpacket = new byte[(n - 4) - size];

            for(int i = 0; i < size; i++)
            {
                payload[i] += receivedbytes[i + 4];

            }

            logthis(cls.clientsock.RemoteEndPoint.ToString());
            logthis(Encoding.ASCII.GetString(payload));
            Array.Copy(receivedbytes, (size + 4), otherpacket, 0, ((n - 4) - size));
            receivedbytes = new byte[(n - 4) - size];
            receivedbytes = otherpacket;
            cls.dataphase = false;
            parse(cls, ((n - 4) - size), receivedbytes); //Send rest of packet to read again
        }

    }
    else
    {
        //not implemented, supposed to handle fragmented packets
        if (n >= cls.datasize)
        {

        }
        else if (n < cls.datasize)
        {

        }

    }

}

共有1个答案

秦焱
2023-03-14

你的问题来自

else
{
    //not implemented, supposed to handle fragmented packets

我会把钱放在这样一个事实上,即您正在访问else语句并丢失数据。一旦您没有完全读取从read函数返回的一个或两个集总在一起的数据包(这比您想象的要常见得多),您的客户机就会陷入cls.dataphase=true;中,永远无法摆脱它。

 类似资料:
  • 但我只能得到: 当请求实体的内容类型不是application/x-www-form-urlencoded时,使用@FormParam 我的代码:

  • 我使用Java11和Spring。我试图通过post将两个对象发送到控制器,但我得到了一个错误。 这里是控制器: 你知道为什么我会得到这个错误以及如何修复它吗?

  • 我在java中创建了一个在桌面上执行时可以正常工作的Swing游戏。但是,由于不可预见的事件,我的老板现在要求将游戏变成一个小程序,以便可以嵌入到网站中(不,JWS不是一个选项)。 以下是我将游戏转换为小程序的步骤: 1)将我的main类更改为具有public ulureinit()而不是public静态ululemain(),并从Applet扩展它 2) 更改了我的myJFrame。java类现

  • iOS 7之前的应用程序工作正常。我今天用Xcode 6尝试了它,当我运行它时,我有一个令人不快的惊喜:(: 你知道iOS 8有什么变化吗?:/我使用了以下定向方法: (N SU整数)支持的接口方向 (BOO L)应该自动旋转 (BOO L)应自动旋转到界面方向:(UI界面方向)界面方向 编辑: 我刚刚发现这个方法: 现在我的第一个视图控制器工作正常:)。问题是当我显示一个模态(改变了UIInte

  • 我有一个关于套接字编程的问题。当我使用套接字发送数据时,我们可以使用诸如sendto()之类的API使用TCP或UDP进行发送。对于sendto(),我们给出一个数组指针和要发送的字节数。 在这种情况下,如果我给出一个大字节数(例如:20000字节),根据我的理解,网络的MTU不会那么大,所以套接字实际上发送多个数据包,而不是一个大数据包。由于这20000字节被分成几个UDP/TCP数据包,但这2

  • 问题内容: 在我的Ajax代码中,我正在向go lang api发送一个关联数组,但是go lang不会接收任何数组。为什么? 为什么这个Ajax不会将数组发送到Go API?在下面的mvc结构中,我想要接收此数据: 问题答案: 您不能直接将数组从客户端发送到服务器,因为数组定义在两侧可能不相同。 有两种解决方法: 一个。您可以在clinet中将数组转换为json字符串,然后将其作为字符串参数发送