简体   繁体   English

WinForms的服务器和客户端之间的通信

[英]Communication between server and client for WinForms

I have 50+ kiosk style computers that I want to be able to get a status update, from a single computer, on demand as opposed to an interval. 我有50多台自助服务终端型计算机,我希望能够从一台计算机上按需获取状态更新,而不是间隔。 These computers are on a LAN in respect to the computer requesting the status. 这些计算机在请求状态的计算机上位于LAN上。

I researched WCF however it looks like I'll need IIS installed and I would rather not install IIS on 50+ Windows XP boxes -- so I think that eliminates using a webservice unless it's possible to have a WinForm host a webservice? 我研究了WCF,但看起来我需要安装IIS,而我宁愿不在50多个Windows XP机箱上安装IIS - 所以我认为除非可以让WinForm主机成为web服务,否则不使用web服务?

I also researched using System.Net.Sockets and even got a barely functional prototype going however I feel I'm not skilled enough to make it a solid and reliable system. 我也研究过使用System.Net.Sockets甚至得到了一个功能不多的原型,但我觉得我不够熟练,无法使它成为一个可靠而可靠的系统。 Given this path, I would need to learn more about socket programming and threading. 鉴于此路径,我需要了解有关套接字编程和线程的更多信息。

These boxes are running .NET 3.5 SP1, so I have complete flexibility in the .NET version however I'd like to stick to C#. 这些盒子运行的是.NET 3.5 SP1,所以我在.NET版本中有完全的灵活性,但是我想坚持使用C#。

What is the best way to implement this? 实现这个的最佳方法是什么? Should I just bite the bullet and learn Sockets more or does .NET have a better way of handling this? 我应该咬紧牙关并更多地学习套接字,还是.NET有更好的方法来处理这个问题?

edit: I was going to go with a two way communication until I realized that all I needed was a one way communication. 编辑:我打算进行双向沟通,直到我意识到我所需要的只是一种单向沟通。

edit 2: I was avoiding the traditional server/client and going with an inverse because I wanted to avoid consuming too much bandwidth and wasn't sure what kind of overhead I was talking about. 编辑2:我正在避开传统的服务器/客户端并使用逆向,因为我想避免消耗太多的带宽,并且不确定我在说什么样的开销。 I was also hoping to have more control of the individual kiosks. 我也希望能够更好地控制个人信息亭。 After looking at it, I think I can still have that with WCF and connect by IP (which I wasn't aware I could connect by IP, I was thinking I would have to add 50 webservices or something). 看了之后,我想我仍然可以使用WCF并通过IP连接(我不知道我可以通过IP连接,我以为我必须添加50个web服务或其他东西)。

WCF does not have to be hosted within IIS, it can be hosted within your Winform, as a console application or as windows service. WCF不必托管在IIS中,它可以作为控制台应用程序或Windows服务托管在Winform中。 You can have each computer host its service within the winform, and write a program in your own computer to call each computer's service to get the status information. 您可以让每台计算机在winform中托管其服务,并在您自己的计算机中编写程序以调用每台计算机的服务以获取状态信息。

Another way of doing it is to host one service in your own computer, and make the 50+ computers to call the service once their status were updated, you can use a database for the service to persist the status data of each node within the network. 另一种方法是在您自己的计算机中托管一个服务,并在更新状态后让50多台计算机调用该服务,您可以使用数据库来保存网络中每个节点的状态数据。 This option is easier to maintain and scalable. 此选项更易于维护和扩展。

PS WCF aims to replace .net remoting, the alternatives can be net.tcp binding or net.pipe PS WCF旨在取代.net远程处理,替代方案可以是net.tcp绑定或net.pipe

Unless you have plans to scale this to several thousand clients I don't think WCF performance will even be a fringe issue. 除非你有计划将其扩展到数千个客户,否则我认为WCF性能甚至不会成为边缘问题。 You can easily host WCF services from windows services or Winforms applications, and you'll find getting something working with WCF will be fairly simple once you get the key concepts. 您可以轻松地从Windows服务或Winforms应用程序托管WCF服务,并且一旦获得关键概念,您将发现使用WCF的工作将非常简单。

I've deployed something similar with around 100-150 clients with great success. 我已经部署了大约100-150个客户端,取得了巨大的成功。

There's plenty of resources out on the web to get you started - here's one to get you going: 网上有大量的资源可以帮助您入门 - 这是一个让您前进的资源:

http://msdn.microsoft.com/en-us/library/aa480190.aspx http://msdn.microsoft.com/en-us/library/aa480190.aspx

Whether you use a web service or WCF on your central server, you only need to install and configure IIS on the server (and not on the 50+ clients). 无论您是在中央服务器上使用Web服务还是WCF,您只需要在服务器上安装和配置IIS(而不是在50+客户端上)。

What you're trying to do is a little unclear from the question, but if the clients need to call the server (to get a server status, for example), then they just call a method on the webservice running on the server. 您要做的是从问题中稍微不清楚,但如果客户端需要调用服务器(例如,获取服务器状态),那么他们只需在服务器上运行的Web服务上调用一个方法。

If instead you need to have the server call the clients from time to time, then you'll need to have each client call a sign-in method on the server webservice each time the client starts up. 如果您需要让服务器不时调用客户端,那么每次客户端启动时,您都需要让每个客户端在服务器webservice上调用登录方法。 The sign-in method would take a delegate method from the client as a parameter. 登录方法将客户端的委托方法作为参数。 The server would then call this delegate when it needed information from the client. 然后,当服务器需要来自客户端的信息时,服务器将调用该委托。

Setting up each client with its own web service would represent an inversion of the traditional (one server, multiple clients) client/server architecture, and as you've already noted this would be impractical. 使用自己的Web服务设置每个客户端将代表传统(一个服务器,多个客户端)客户端/服务器体系结构的反转,正如您已经注意到的那样,这将是不切实际的。

Do not use remoting. 不要使用远程处理。

If you want robustness and scalability you end up ruling out everything but what are essentially stateless remote procedure calls. 如果你想要健壮性和可扩展性,你最终会排除一切,除了基本上无状态的远程过程调用。 Since this is exactly the capability of web services, and web services are simpler and easier to build, remoting is an essentially pointless technology. 由于这正是Web服务的功能,并且Web服务更简单,更易于构建,因此远程处理技术本质上是毫无意义的。

Callbacks with remote delegates are on the performance/reliability forbidden list, so if you were thinking of using remoting for that, think again. 具有远程委托的回调在性能/可靠性禁止列表中,因此如果您考虑使用远程处理,请再想一想。

Use web services. 使用Web服务。

I know you don't want to be polling, but I don't think you need to. 我知道你不想参加投票,但我认为你不需要投票。 Since you say all your units are on a single network segment then I suggest UDP for broadcast change notifications, essentially setting a dirty flag, and allowing the application to (re-)fetch on demand. 既然你说你的所有单元都在一个网段上,那么我建议UDP用于广播变更通知,主要是设置一个脏标志,并允许应用程序按需(重新)获取。 It's still not reliable but it's easy and very fast because it's broadcast. 它仍然不可靠,但它很容易,也很快,因为它是广播。

As others have said you don't need IIS, you can self-host. 正如其他人所说,你不需要IIS,你可以自我托管。 See ServiceHost class for details on how to do this. 有关如何执行此操作的详细信息,请参阅ServiceHost类

I'd suggest using .NET Remoting. 我建议使用.NET Remoting。 It's quite easy to implement and doesn't require anything else. 它很容易实现,不需要任何其他东西。

For me its is better to learn networking.. or the manual way of socket communication.. web services are mush slower because it contains metadata.. 对我来说,它更好地学习网络..或手动的套接字通信方式..网络服务速度慢,因为它包含元数据..

your clients and the servers can transform to multithreaded application. 您的客户端和服务器可以转换为多线程应用程序。 just imitate the request and response architecture. 只是模仿请求和响应架构。 it is much easy to implement a network application like this.. 实现像这样的网络应用程序要容易得多。

If you just need a status update, you can use much simpler solution, such as simple tcp server/client messaging or like orrsella said, remoting. 如果您只需要状态更新,则可以使用更简单的解决方案,例如简单的tcp服务器/客户端消息传递或类似orrsella所说的远程处理。 WCF is kinda overkill here. WCF在这里有点矫枉过正。

One note though, if all your 50+ kiosk is connected via internet, then you might need use VPN or have an open port on each kiosk(which is a security risk) so that your server can retrieve status update from each kiosk. 但需要注意的是,如果您的所有50多个信息亭都通过互联网连接,那么您可能需要使用VPN或在每个信息亭上都有一个开放端口(这是一个安全风险),以便您的服务器可以从每个信息亭检索状态更新。

We had a similiar situation, but the status is send to our server periodically, so we only have 1 port to protect/secure. 我们有一个类似的情况,但状态定期发送到我们的服务器,所以我们只有1个端口来保护/保护。 The frequency of the update is configurable as to accomodate slower clients. 更新的频率可配置为容纳较慢的客户端。

As someone who implemented something like this with over 500+ clients and growing: 作为一个与超过500个客户实现类似的东西并且不断增长的人:

Message Queing is the way to go. Message Queing是要走的路。

We have gone from an internal developed TCP server and client to WCF polling and ended up with Message queing. 我们已从内部开发的TCP服务器和客户端转到WCF轮询,最终得到了消息队列。 It's the only guaranteed way to get data to and from clients and servers over the internet. 这是通过互联网从客户端和服务器获取数据的唯一保证方式。 As a bonus, many of these solutions have an extensive framework makeing it trivial to implement publish-subscribe, Send-one-way, point-to-point sending, Request-reply. 作为奖励,许多这些解决方案都有一个广泛的框架,使得实现发布 - 订阅,发送 - 单向,点对点发送,请求 - 回复变得微不足道。 Some of these are possible with WCF but it will involve crying, shouting, whimpering and long nights not to mention gallons of coffee. 其中一些是可能的WCF,但它将涉及哭泣,喊叫,呜咽和长夜,更不用说加仑咖啡。

A couple of important remarks: 几个重要的评论:

Letting a process poll the clients instead of the other way around = Bad idea.. it is not scalable at all and you will soon be running in to trouble when the process is take too long to complete.. Not to mention having to handle all the ip addresses ( do you have access to all clients on the required ports ? What happpens when the ip changes etc..) 让一个进程轮询客户端而不是反过来=糟糕的想法..它根本不可扩展,你很快就会遇到麻烦,因为这个过程需要很长时间才能完成..更不用说必须处理所有ip地址(你有权访问所需端口上的所有客户端吗?当ip改变等等时会有什么好处。)

what we have done: The clients sends status updates to a central message queue on a regular interval ( you can easily implement live updates in the UI), it also listens on it's own queue for a GetStatusRequest message. 我们做了什么:客户端定期将状态更新发送到中央消息队列(您可以在UI中轻松实现实时更新),它还会在其自己的队列中侦听GetStatusRequest消息。 if it receives this, it answers ( has a timeout).. this way, we can see overal status of all clients at all times and get a specific status of a specific client when needed. 如果它收到这个,它会回答(有一个超时)..这样,我们可以随时查看所有客户端的总体状态,并在需要时获得特定客户端的特定状态。

Concerning bandwidth: kiosk usually show images/video etc.. 1Kb or less status messages will not be the big overhead. 关于带宽:信息亭通常显示图像/视频等.1Kb或更少的状态消息不会是很大的开销。

I CANNOT stress enough that the current design you present will have a very intensive development cycle AND will not scale or extend well ( trust me, we have learned this lesson). 我不能强调你所呈现的当前设计将具有非常密集的开发周期并且不会扩展或扩展(相信我,我们已经学到了这一课)。 Next to this, building a good client/server protocol for this type of stuff is a hard job that will be totally useless afterwards if you make a design error ( migrating a protocol is not easy) 接下来,为这种类型的东西构建一个好的客户端/服务器协议是一项艰巨的工作,如果你犯了设计错误(迁移协议并不容易),那将是完全没用的

We have built our solution ontop of ActiveMQ ( using NMS library c#) and are currently extending Simple Service Bus for our internal workings. 我们在ActiveMQ(使用NMS库c#)上构建了我们的解决方案,目前正在为我们的内部工作扩展简单服务总线。

We only use WCF for the communication between our winforms app and the centralized service(s) 我们只使用WCF进行winforms应用程序和集中服务之间的通信

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM