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.
Instead of reinventing the wheel, use serialization
You have many alternatives for this (JavaScriptSerializer, DataContractSerializer, DataContractJsonSerializer, BinaryFormatter, SoapFormatter, XmlSerializer).
»EDIT«
Assuming
NwisNetworkStream, your code can be as simple as like this: