C# 如何根据it将传入数据包拆分为';s型

C# 如何根据it将传入数据包拆分为';s型,c#,string,visual-studio,C#,String,Visual Studio,我的服务器正在运行,它正在从客户端接收以下消息 string message=2001 24 9228 2323385135 202 Name Account 25.97.64.180 1_66_123 - - - -3001 24 302 Name RECEIVERNAME Account 1_151_122 hello6001 24 Name Account 165 3103069 6353069 575839004 602 如何拆分此邮件以获得单独的邮件,如 string message

我的服务器正在运行,它正在从客户端接收以下消息

string message=2001 24 9228 2323385135 202 Name Account 25.97.64.180 1_66_123 - - - -3001 24 302 Name RECEIVERNAME Account 1_151_122 hello6001 24 Name Account 165 3103069 6353069 575839004 602
如何拆分此邮件以获得单独的邮件,如

string message1=2001 24 9228 2323385135 202 Name Account 25.97.64.180 1_66_123 - - - -(it always contains 13 strings)
string message2=3001 24 302 Name RECEIVERNAME Account 1_151_122 hello (it always contains 8 strings)
string message3=6001 24 Name Account 165 3103069 6353069 575839004 602(it always contain 9 strings)
因为它的服务器总是很难获得单独的消息,有什么解决方案吗

到目前为止,我得到的代码在一个包中处理相同类型的消息

   string data=2001 24 0 0 210 Name Account 124.123.100.99 61_60_85 name account 124.123.100.99 2116653670_02001 24 9646 1053821743 207 name account25.97.64.180 1_149_128 TorrBier - - - 

          string Packet_Type = data.Substring(0, 4);

            string[] Packet_Split = Regex.Split(data, Packet_Type + " 24 ");

            List<string> Seperate_Packets = new List<string>();


            foreach (string packetstrings in Packet_Split)
            {

                if (packetstrings.Trim().Length > 0)
                    Seperate_Packets.Add(Packet_Type + " 24 " + "" + packetstrings.Trim());

            }
有助于使用不同类型正确分隔邮件

关于更多信息,这就是数据包的含义

2001 24 0 0 210 Name Account 25.97.64.180 1_155_126 Name Account 25.97.64.180 3722785844_1000
                    /// 
                    /// Part 1 : 2001 Packet Type
                    /// Part 2 : 24 Server ID
                    /// Part 3 : Item Code
                    /// Part 4 : Uniq number
                    /// Part 5 : Action Type - IMP
                    /// Part 6 : Actor Character Name
                    /// Part 7 : Actor Character Account
                    /// Part 8 : Actor IP Address
                    /// Part 9 : Actor Location
                    /// Part 10 : Receiver Character name  / NPC Name
                    /// Part 11: Receiver Account
                    /// Part 12: Receiver IP Address
                    /// Part 13: Item OPTIONS DETAILS

此模式对于每个数据包都是不同的

每个消息的第一部分(2001、3001、6001)可以用作消息类型的标识符。当您确定消息类型时,您就知道要形成一条消息还需要多少字符串。

假设消息块总是以相同数量的字符串和相同的顺序出现,而这可能不是最优雅的解决方案,您可以将空格拆分为列表或数组,并使用stringbuilder创建消息,差不多

for(int i=0; i<message.length;i++)
{
   if(i>=0 && < 13){ [populate message 1]; }
   if(i>=13 && < 21){ [populate message 2]; }
   if(i>=21 && < message.length){ [populate message 3]; }
}
for(int i=0;i=0&&<13){[populatemessage 1];}
如果(i>=13&&<21){[填充消息2];}
如果(i>=21&
我很快编写了它,但没有对其进行优化。大量重复的代码。无论如何,即使没有完全测试它,它也能工作。以此为出发点

附言。 您可以通过在正确的位置添加som TrimStart()来增强它

    public static IList<string> Parser(string srvString)
    {
        IList<string> list = new List<string>();

        while (!string.IsNullOrWhiteSpace(srvString))
        {
            string[] split = srvString.Split();
            string code = srvString.Split()[0];
            switch (code)
            {
                case "2001":
                    int i = 13;
                    if (i >= split.Length)
                    {
                        // we are a the end of the server message
                        list.Add(srvString);
                        srvString = null;
                    }
                    else
                    {
                        string last = split[i - 1];
                        Match match = Regex.Match(last, "(2001|3001|6001)");
                        if (!match.Success) throw new Exception("Parsing error");
                        string msg = srvString.Substring(0, srvString.IndexOf(last, code.Length) + last.Length - 4);
                        list.Add(msg);
                        srvString = srvString.Substring(msg.Length);
                    }
                    break;

                case "3001":
                    i = 8;
                    if (i >= split.Length)
                    {
                        // we are a the end of the server message
                        list.Add(srvString);
                        srvString = null;
                    }
                    else
                    {
                        string last = split[i - 1];
                        Match match = Regex.Match(last, "(2001|3001|6001)");
                        if (!match.Success) throw new Exception("Parsing error");
                        string msg = srvString.Substring(0, srvString.IndexOf(last, code.Length) + last.Length - 4);
                        list.Add(msg);
                        srvString = srvString.Substring(msg.Length);
                    }
                    break;

                case "6001":
                    i = 9;
                    if (i >= split.Length)
                    {
                        // we are a the end of the server message
                        list.Add(srvString);
                        srvString = null;
                    }
                    else
                    {
                        string last = split[i - 1];
                        Match match = Regex.Match(last, "(2001|3001|6001)");
                        if (!match.Success) throw new Exception("Parsing error");
                        string msg = srvString.Substring(0, srvString.IndexOf(last, code.Length) + last.Length - 4);
                        list.Add(msg);
                        srvString = srvString.Substring(msg.Length);
                    }
                    break;

                default:
                    throw new Exception("Unkown message code: " + code);

            }   // switch

        }   // while...

        // return the list
        return list;
    }
公共静态IList解析器(字符串srvString)
{
IList list=新列表();
而(!string.IsNullOrWhiteSpace(srvString))
{
string[]split=srvString.split();
字符串代码=srvString.Split()[0];
开关(代码)
{
案例“2001”:
int i=13;
如果(i>=split.Length)
{
//我们是服务器消息末尾的一个用户
添加(srvString);
srvString=null;
}
其他的
{
字符串last=split[i-1];
Match Match=Regex.Match(最后一个“(2001 | 3001 | 6001)”;
如果(!match.Success)抛出新异常(“解析错误”);
字符串msg=srvString.Substring(0,srvString.IndexOf(last,code.Length)+last.Length-4);
添加(msg);
srvString=srvString.Substring(msg.Length);
}
打破
案例“3001”:
i=8;
如果(i>=split.Length)
{
//我们是服务器消息末尾的一个用户
添加(srvString);
srvString=null;
}
其他的
{
字符串last=split[i-1];
Match Match=Regex.Match(最后一个“(2001 | 3001 | 6001)”;
如果(!match.Success)抛出新异常(“解析错误”);
字符串msg=srvString.Substring(0,srvString.IndexOf(last,code.Length)+last.Length-4);
添加(msg);
srvString=srvString.Substring(msg.Length);
}
打破
案例“6001”:
i=9;
如果(i>=split.Length)
{
//我们是服务器消息末尾的一个用户
添加(srvString);
srvString=null;
}
其他的
{
字符串last=split[i-1];
Match Match=Regex.Match(最后一个“(2001 | 3001 | 6001)”;
如果(!match.Success)抛出新异常(“解析错误”);
字符串msg=srvString.Substring(0,srvString.IndexOf(last,code.Length)+last.Length-4);
添加(msg);
srvString=srvString.Substring(msg.Length);
}
打破
违约:
抛出新异常(“未知消息代码:”+代码);
}//开关
}//当。。。
//返回列表
退货清单;
}

您自己已经试过了吗?如果是这样的话,你的代码是什么样子的?我为相同类型的数据包编写了代码,其中它只拆分一个包含2001的数据包,比如wise
string packet\u type=data.Substring(0,4);string[]Packet_Split=Regex.Split(主数据,Packet_Type+“24”);List separate_Packets=新列表()它是什么意思:“它总是包含13个字符串”,“它总是包含8个字符串”,等等。?如果我捕捉到它,第一个选项应该是“它总是包含16个字符串”,因为4个虚线“--”之间有空格如果数据包是2001,那么它是一个模式客户端发送给我。它的完整表达式类似于
200120210名称帐户25.97.64.180 1_155_126名称帐户25.97.64.180 3722785844_1000
,因此根据客户端的操作,数据包在末尾添加
-
。字符串“25.97.64.180”是一个令牌
    public static IList<string> Parser(string srvString)
    {
        IList<string> list = new List<string>();

        while (!string.IsNullOrWhiteSpace(srvString))
        {
            string[] split = srvString.Split();
            string code = srvString.Split()[0];
            switch (code)
            {
                case "2001":
                    int i = 13;
                    if (i >= split.Length)
                    {
                        // we are a the end of the server message
                        list.Add(srvString);
                        srvString = null;
                    }
                    else
                    {
                        string last = split[i - 1];
                        Match match = Regex.Match(last, "(2001|3001|6001)");
                        if (!match.Success) throw new Exception("Parsing error");
                        string msg = srvString.Substring(0, srvString.IndexOf(last, code.Length) + last.Length - 4);
                        list.Add(msg);
                        srvString = srvString.Substring(msg.Length);
                    }
                    break;

                case "3001":
                    i = 8;
                    if (i >= split.Length)
                    {
                        // we are a the end of the server message
                        list.Add(srvString);
                        srvString = null;
                    }
                    else
                    {
                        string last = split[i - 1];
                        Match match = Regex.Match(last, "(2001|3001|6001)");
                        if (!match.Success) throw new Exception("Parsing error");
                        string msg = srvString.Substring(0, srvString.IndexOf(last, code.Length) + last.Length - 4);
                        list.Add(msg);
                        srvString = srvString.Substring(msg.Length);
                    }
                    break;

                case "6001":
                    i = 9;
                    if (i >= split.Length)
                    {
                        // we are a the end of the server message
                        list.Add(srvString);
                        srvString = null;
                    }
                    else
                    {
                        string last = split[i - 1];
                        Match match = Regex.Match(last, "(2001|3001|6001)");
                        if (!match.Success) throw new Exception("Parsing error");
                        string msg = srvString.Substring(0, srvString.IndexOf(last, code.Length) + last.Length - 4);
                        list.Add(msg);
                        srvString = srvString.Substring(msg.Length);
                    }
                    break;

                default:
                    throw new Exception("Unkown message code: " + code);

            }   // switch

        }   // while...

        // return the list
        return list;
    }