Click here to Skip to main content
15,887,355 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
I have a server app and a client app and I need to send an array of strings between the two. Is there a specific method for sending arrays from servers.
I have tried looping through each string in an array and sending them individually however I'm not sure how to make the client receive the data properly.
Any advice on how to do this would be a great help.

Update from OP:
They use TCP. Here's the code I am using to try to send an array
C#
List<string> test = new List<string> { };
test.Add("test");
test.Add("test2");
test.Add("test3");
TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(IPAddress.Any), 3000);
client.Connect(serverEndPoint);
NetworkStream clientStream = client.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
for (int i = 0; i < test.Count; i++)
{
byte[] buffer = encoder.GetBytes(test[i]);
clientStream.Write(buffer, 0, buffer.Length);
}
//byte[] buffer = encoder.GetBytes("Hello Server!");

clientStream.Flush();
</string></string>

I hope that helps.
Posted
Updated 17-Oct-10 22:42pm
v2
Comments
dmageiras 17-Oct-10 17:47pm    
Could you specify how client and server communicate?
Neil Cross 18-Oct-10 4:39am    
I entered my information as an answer by accident but I'm not sure you get notified of it so just in case I'll let you know I added the information as an answer. If you do get notified about it then my bad.
Sandeep Mewara 18-Oct-10 4:40am    
Using 'Improve Question' for editing/updating keeps it in more readable and understandable format.

1 solution

There a few ways to skin this problem (you could use web /WCF services for example).

If you want to transfer point to point, you can use the defaultserializers namespace[^], which provide classes that take over the task of converting the [array] object to a string, then you can deserialize back to an array. This requires a single tcp transfer. The essential thing is that the client and server use the same formatter. For your string a Binary Formatter[^] should be sufficient. There is an example in the last link, but in summary:
C#
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(myStream, myArray);

Then on the server:

C++
BinaryFormatter formatter = new BinaryFormatter();
string[] desrialized = formatter.Deserialize(inputStream) as string[];
 
Share this answer
 
Comments
Dalek Dave 18-Oct-10 5:43am    
Good Call, Keith.
E.F. Nijboer 18-Oct-10 5:48am    
Good answer, but also be aware that the built-in formatters don't have the best performance and efficiency. Have a look at these links if the standard serialization objects are causing a performance issues.
http://weblogs.asp.net/vga/archive/2004/05/11/DontLetTheBinaryFormatterGetAtIt.aspx
http://www.codeproject.com/KB/cs/SerializationOne.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900