0

I'm using TCPClient to send message (List of arrays) over LAN, so I have separated:

  • array elements with combination: string arr_sep = "[s{p(a)c}e]";
  • list elements with combination: string list_sep = "[n|e|w)";

How to decipher the following string line to List<string[]> using the regex?

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";

Here is what I tried to do:

        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!

Unfortunately, Regex is completely broken - how do I fix it? Is there a better approach maybe?

Expected result:

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

EDIT: fix

When I receive the data - I normally limit the bytes to 1024, however that's not enough to get all 50 entries of List<string[]>!

I increased the number of bytes up to 10000 and now all info goes through LAN! It takes 3499 bytes to serialize 50 string[] of List<string[]>. In the future I will be using up to 900 entries in my List, so it is safe to assume that I will need:

(3499/50)*900 = 63000 bytes to serialize my data!!

the question is - is it safe/secure to send that must data at once? Here is the code that I use to receive:

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

And that's to send:

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

Can I increase the maximum amount of bytes to 100 thousands and forget about this problem once and for all? There should be another solution... i believe.

4
  • 3
    read about serialization
    – VladL
    Jan 11, 2013 at 21:01
  • 1
    what result are you expecting from your input string?
    – seeker
    Jan 11, 2013 at 21:01
  • @seeker Result should be similar to this: new List<string[]>(new string[]{"abra", "kada", "bra"}, new string[]{"hel", "oww", "een"})
    – Alex
    Jan 11, 2013 at 21:03
  • In general, I would pick a more robust, established and supported message exchange format.
    – Ryan Gates
    Jan 11, 2013 at 21:19

3 Answers 3

2

Instead of reinventing the wheel, use serialization

You have many alternatives for this (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);

»EDIT«

Assuming Nw is NetworkStream, your code can be as simple as like this:

//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();
}
6
  • I get "unterminated string passed in" error on List<string[]> result. I tried to remove special characters (before send) but it didn't solve the problem: Regex.Replace(HttpUtility.HtmlDecode(sub), @"[^\w\s:^-]", "", RegexOptions.IgnorePatternWhitespace)
    – Alex
    Jan 11, 2013 at 21:41
  • @Alex, Don't try to process string before sending. Just use it as is. All Serializers can easily deserialize the output they produce.
    – I4V
    Jan 11, 2013 at 21:48
  • Well, yes, processing didn't fix the problem, so why am I getting "Unterminated string passed in ..." error?
    – Alex
    Jan 11, 2013 at 21:51
  • If it is at runtime, I'll guess, your receving code is buggy. If you try the above sample without networking (with any string) you will see that it is working. Maybe you want to show what you are doing?
    – I4V
    Jan 11, 2013 at 21:53
  • OK! The problem existed due to the high amount of data - I couldn't receive all of it! Can you please look at my edit and answer the question? I'm not sure if i will be able to send data using JavaScriptSerializer() in the future (because of problem that I've described in "edit")
    – Alex
    Jan 11, 2013 at 23:00
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\]"));
    }
}

This will give you the desired output you described.

0
0

If you're sure that you don't want to use any existing protocol or technology, you can easily invent your own protocol using the BinaryWriter and BinaryReader classes:

Sender:

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

Receiver:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.