通过服务器发送阵列 [英] Sending an array over a server

查看:80
本文介绍了通过服务器发送阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器应用程序和一个客户端应用程序,我需要在两者之间发送一个字符串数组.是否有用于从服务器发送阵列的特定方法.
我尝试遍历数组中的每个字符串并分别发送它们,但是我不确定如何使客户端正确接收数据.
有关如何执行此操作的任何建议将对您有很大帮助.

从OP更新:
他们使用TCP.这是我用来尝试发送数组的代码

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

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.

推荐答案

有几种方法可以解决此问题(例如,可以使用Web/WCF服务).

如果要点对点传输,则可以使用默认的序列化器名称空间 [ ^ ],其中提供接管将[array]对象转换为字符串的任务的类,然后可以反序列化回数组.这需要 tcp传输.重要的是,客户端和服务器使用相同的格式化程序.对于您的字符串,二进制格式化程序 [ ^ ]应该足够了.最后一个链接中有一个示例,但总结如下:
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:
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(myStream, myArray);


然后在服务器上:


Then on the server:

BinaryFormatter formatter = new BinaryFormatter();
string[] desrialized = formatter.Deserialize(inputStream) as string[];


这篇关于通过服务器发送阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆