0

TCPClientを使用してLAN経由でメッセージ(配列のリスト)を送信しているので、次のように分離しました。

  • 組み合わせのある配列要素:string arr_sep = "[s{p(a)c}e]";
  • 組み合わせで要素を一覧表示します。string list_sep = "[n|e|w)";

正規表現stringを使用するために次の行を解読するにはどうすればよいですか?List<string[]>

string tessst = "abra[s{p(a)c}e]kada[s{p(a)c}e]bra[n|e|w)hel[s{p(a)c}e]oww[s{p(a)c}e]een";

これが私がやろうとしたことです:

        string tessst = "abra[s{p(a)c}e]kada[s{p(a)c}e]bra[n|e|w)hel[s{p(a)c}e]oww[s{p(a)c}e]een";
        List<string[]> splited2 = new List<string[]>();

        if (tessst.Length > 0)
        {
            List<string> splited1 = new List<string>(Regex.Split(tessst, "[^a-zA-Z]+")); //[s{p(a)c}e]

            for (int i = 0; i < splited1.Count; i++)
            {
                splited2.Add(Regex.Split(splited1[i], "[^a-zA-Z]+")); // [n|e|w)
            }
        }
        //splited2  is the result!

残念ながら、正規表現は完全に壊れています-どうすれば修正できますか?より良いアプローチはありますか?

期待される結果:

List<string[]> result = new List<string[]>();
result.Add(new string[]{"abra", "kada", "bra"});
result.Add(new string[]{"hel", "oww", "een"});

編集:修正

List<string[]>データを受信すると、通常はバイトを1024に制限しますが、 !の50エントリすべてを取得するにはそれだけでは不十分です。

バイト数を10000まで増やしたところ、すべての情報がLANを経由するようになりました。string[]の50をシリアル化するには3499バイトかかりますList<string[]>。将来的には、リストで最大900のエントリを使用する予定なので、次のものが必要になると想定しても問題ありません。

(3499/50)* 900=63000バイトでデータをシリアル化!!

問題は、データを一度に送信するのは安全/安全ですか?これが私が受け取るために使用するコードです:

string message = "";
int thisRead = 0;
int max = 10000; // from 1024 to 10000
Byte[] dataByte = new Byte[max];
using (var strm = new MemoryStream())
{
  thisRead = Nw.Read(dataByte, 0, max);
  strm.Write(dataByte, 0, thisRead);
  strm.Seek(0, SeekOrigin.Begin);

  using (StreamReader reader = new StreamReader(strm))
  {
     message = reader.ReadToEnd();
  }

}
List<string[]> result = new JavaScriptSerializer().Deserialize<List<string[]>>(message );

そしてそれは送ることです:

List<string[]> list= new List<string[]>();
list = browser_ex.GetMusicListSer(); // 50 list elements
string text_message = new JavaScriptSerializer().Serialize(list);
MemoryStream Fs = new MemoryStream(ASCIIEncoding.Default.GetBytes(text_message));
Byte[] buffer = Fs.ToArray();
Nw.Write(buffer, 0, buffer.Length); // 3499 bytes

最大バイト数を10万バイトに増やして、この問題を完全に忘れることはできますか?別の解決策があるはずです...私は信じています。

4

3 に答える 3

2

車輪の再発明の代わりに、シリアル化を使用する

これには多くの選択肢があります(JavaScriptSerializer、DataContractSerializer、DataContractJsonSerializer、BinaryFormatter、SoapFormatter、XmlSerializer)。

List<string[]> list = new List<string[]>();
list.Add(new string[] { "abra", "kada", "bra" });
list.Add(new string[] { "hel", "oww", "een" });

string stringToSend = new JavaScriptSerializer().Serialize(list);
//Send 
string receivedString = stringToSend;
List<string[]> result = new JavaScriptSerializer()
                            .Deserialize<List<string[]>>(receivedString);

"編集"

であると仮定するNwNetworkStream、コードは次のように単純にすることができます。

//Receiver
StreamReader reader = new StreamReader(Nw);
while (true)
{
    List<string[]> result = new JavaScriptSerializer()
                .Deserialize<List<string[]>>(reader.ReadLine());

    //do some work with "result"

}

//Sender
StreamWriter writer = new StreamWriter(Nw);
while (true)
{
    //form your "list" and send
    writer.WriteLine(new JavaScriptSerializer().Serialize(list));
    writer.Flush();
}
于 2013-01-11T21:12:36.310 に答える
1
string tessst = "abra[s{p(a)c}e]kada[s{p(a)c}e]bra[n|e|w)hel[s{p(a)c}e]oww[s{p(a)c}e]een";
List<string[]> splited2 = new List<string[]>();

if (tessst.Length > 0)
{
    List<string> splited1 = new List<string>(Regex.Split(tessst, @"\[n\|e\|w\)")); 

    for (int i = 0; i < splited1.Count; i++)
    {
        splited2.Add(Regex.Split(splited1[i], @"\[s\{p\(a\)c\}e\]"));
    }
}

これにより、説明した目的の出力が得られます。

于 2013-01-11T21:18:30.377 に答える
0

既存のプロトコルやテクノロジーを使用したくない場合は、BinaryWriterクラスとBinaryReaderクラスを使用して独自のプロトコルを簡単に作成できます。

送信者:

using (var writer = new BinaryWriter(networkStream))
{
    foreach (var array in list)
    {
        writer.Write(array.Length);
        for (int i = -; i < array.Length; i++)
        {
            writer.Write(array[i]);
        }
    }
    writer.Write(0);
    writer.Flush();
}

受信者:

using (var reader = new BinaryReader(networkStream))
{
    var list = new List<string[]>();
    int length;
    while ((length = reader.ReadInt32()) != 0)
    {
         var array = new string[length];
         for (int i = 0; i < array.Length; i++)
         {
             array[i] = reader.ReadString();
         }
         list.Add(array);
    }
}
于 2013-01-11T21:10:47.230 に答える