将数据客户端发布到服务器的最佳方式 C#

发布于 2024-10-31 16:52:48 字数 204 浏览 0 评论 0原文

我目前正在开发 Windows 客户端,使用 Windows 窗体应用程序从 Windows 计算机收集一些数据。我需要将此数据发送到服务器。但我不知道最好的方法是什么。我目前正在尝试 WCF Web 服务来获取数据并返回 true 或 false。但我需要学习将数据发送到服务器的最快方法。客户必须可靠且快速。我的选择是什么或最好的方法是什么。服务器仅发送回 true 或 false 数据。

I'm currently developing windows client using Windows Form Application collecting some data from Windows machine. I need to send this data to server. But I don't know what is the best way to do that. I'm currently trying to WCF Web Service to get data and return true or false. But i need to learn fastest way to send data to server. The client must be reliable and fast. What is my options or best way to do that. The server only sends back data as true or false.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔嚣张 2024-11-07 16:52:48

如果我有这样的任务,我也会使用 WCF Web 服务。

我唯一要做的区别是:输入 void 并在出现错误时抛出异常。

If I had such task, I would use WCF web service as well.

The only one difference I would make: type void and throw exception in case of error.

南街九尾狐 2024-11-07 16:52:48

我会看一下 RhinoServiceBus。它速度快并且相当容易实现。如果您不喜欢这样,那么我也会使用 WCF。

I would take a look at RhinoServiceBus. It is fast and fairly easy to implement. If you don't like that then I would be using WCF as well.

风为裳 2024-11-07 16:52:48

您可以使用基于套接字的低级网络传输协议(例如 TCP 或 UDP),但是您必须自己管理转换和序列化。

在 C# 中,您将使用 TcpClient 和 TcpListener 类,并使用某种序列化程序(本例中为 BinaryFormatter)序列化您的对象

ServerCode:

...
TcpListener listener = new TcpListener(8080);
listener.Start();
using  (TcpClient client = listener.AcceptTcpClient())
{
    BinaryFormatter formatter = new BinaryFormatter();
    //Assuming the client is sending an integer
    int arg = (int)formatter.Deserialize(client.GetStream());
    ... //Do something with arg
    formatter.Serialize(result); //result is your boolean answer
} 
...

ClientCode:

 ...
   using (TcpClient client = new TcpClient(ipaddress, 8080) //ipaddress is the ip address of the server
  {
   BinaryFormatter formatter = new BinaryFormatter();
   formatter.Serialize(client.GetStream(), 12) //12 is an example for the integer
   bool result = formatter.Deserialize(client.GetStream());
   ... //do something with result
  }
   ...

但如您所见,最快(UDP 可能更快,但不能保证发送数据) )方法并不是最简单的(也不总是最好的)。

因此,对于 Windows 窗体项目,我会使用某种“现成的”RMI/RPC API,例如 WCF 或 ASP.Net Web 服务

You can use a low level network transport protocol based on Sockets like TCP or UDP, but then you have to manage conversion and serialization yourself.

In C# you would use TcpClient and TcpListener classes, and serialize your objects with some kind of serializer (BinaryFormatter in this example)

ServerCode:

...
TcpListener listener = new TcpListener(8080);
listener.Start();
using  (TcpClient client = listener.AcceptTcpClient())
{
    BinaryFormatter formatter = new BinaryFormatter();
    //Assuming the client is sending an integer
    int arg = (int)formatter.Deserialize(client.GetStream());
    ... //Do something with arg
    formatter.Serialize(result); //result is your boolean answer
} 
...

ClientCode:

 ...
   using (TcpClient client = new TcpClient(ipaddress, 8080) //ipaddress is the ip address of the server
  {
   BinaryFormatter formatter = new BinaryFormatter();
   formatter.Serialize(client.GetStream(), 12) //12 is an example for the integer
   bool result = formatter.Deserialize(client.GetStream());
   ... //do something with result
  }
   ...

But as you can see, the fastest(UDP may be faster, but does not guarantee sending the data) way is not the easiest (and not always the best).

So for an windows forms project I would use some kind of "ready-made" RMI/RPC API like WCF or ASP.Net web services

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文