C# 서버 및 클라이언트 통신 패킷 처리

    void ReceiveBack()
    {
        int index = 0;                                           // 
        byte[] arr1 = new byte[2048];
        List arr3 = new List();                      //  
        int totalLength = 0;// 
        int variableCount = 0;// 
        int nRemainAndNew = 0;// 
        SocketConnetBack();

        while (true)
        {
            recvLen = clientSocket.Receive(recvData);// 
            index = 0;
            for (int k = 0; k < recvLen; k++)
            {
                arr3.Add(recvData[k]);
            }

            nRemainAndNew = arr3.Count;
            arr1 = arr3.ToArray();
            totalLength = BitConverter.ToInt32(arr1, index);// 
            double remainDataLength = nRemainAndNew;
            while (totalLength <= remainDataLength)
            {
                //  
                index += 4;
                variableCount = BitConverter.ToInt32(arr1, index);// 
                index += 4;
                string szVariables = Encoding.ASCII.GetString(arr1, index, variableCount);// 
                string[] arrVariables = szVariables.Split('#');// 
                index += variableCount;
                this.inputCountnormal = BitConverter.ToInt32(arr1, index);
                index += 4;
                this.varInputListnormal.Clear();
                this.varInputNameListnormal.Clear();
                for (int i = 0; i < this.inputCountnormal; i++)
                {
                    double a = BitConverter.ToDouble(arr1, index);
                    index += 8;
                    VariableAllInfo vai = new VariableAllInfo();
                    vai.dataType = 0;//double
                    vai.strName = arrVariables[i];
                    vai.dValue = a;
                    this.varInputListnormal.Add(vai);
                    this.varInputNameListnormal.Add(vai.strName);
                }
                    
                //  
                remainDataLength -= totalLength;
                if (remainDataLength > 4)
                {
                    totalLength = BitConverter.ToInt32(arr1, index);
                }
                else
                {
                    break;
                }
            }
                
            //  
            if (remainDataLength > 0)
            {
                int nArray3Count = arr3.Count;
                arr3.Clear();
                for (int j = index; j < nArray3Count; j++)
                {
                    arr3.Add(arr1[j]);
                }
            }
            else
            {
                arr3.Clear();
            }            
       }
}

좋은 웹페이지 즐겨찾기