- Home /
How do I send an array over the network?
Ive got an array of (string) player names that exist in my masterAdmiServer. I need the masterAdminServer to send the names to the player so the player can check to see if the name they entered is a registered username. I know how to send a single string over the network, but I need to send string[] Names; to the players. Ive tried it several different ways and get a variety of errors. C#
Answer by fafase · Nov 12, 2014 at 06:52 AM
Make your array a single string by concatenating them all in one:
void SendArray(string[] str)
{
StringBuilder sb = new StringBuilder("");
foreach(string s in str)
{
sb.Append(s+"_");
}
string newString = sb.ToString();
// Send your string over the network
}
Then you can retrieve on the other side:
string [] SplitToArray(string str)
{
return str.Split('_');
}
So it combines all the strings in the array into 1 string, and in that string, each piece of the array is separated by a "_". The massive string is sent over the network and the player turns the long string back into an array.
That's how it works, right?
Right click on StringBuilder then Resolve. It is probably missing System.Text
ok, but how is it splitting the string back into an array. I mean, it splits the string, but I don't see where it puts that into an array
in the receiving script:
string [] info = null;
void Receive(string str)
{
info = SplitToArray(str);
}
or just:
string [] info = null;
void Receive(string str)
{
info = str.Split('_');
}
Answer by ashique · Nov 12, 2014 at 05:36 AM
Hey..we can't send an array over network, thats why we are having collections, try to send an Object of collection like List, Set.. Thanks
Your answer
Follow this Question
Related Questions
Iterating through multidimensional arrays 2 Answers
Get JSON array object string value 2 Answers
Distribute terrain in zones 3 Answers
Value will not return from ArrayList, C# 1 Answer
String Parsing 1 Answer