就我而言,用 C# 编写服务器应用程序是否合理?

发布于 2024-09-11 00:18:48 字数 1744 浏览 4 评论 0原文

  • 我希望它能够在 Windows 服务器上运行。
  • 它将是一个云类型服务器 - 它将由运行在世界各地不同机器上的模块\部分组成,使用http\tcp + upnp相互连接
  • 每台机器上将有控制\监控\观察模块提供性能统计数据
  • 该网络将处理大量视频\音频生活流\广播数据
  • 它将使用 FFMPEG 进行重新编码,使用 OpenGL、OpenCV 等进行过滤(顺便说一句,.NET 包装器存在并工作)
  • 它不会使用任何 WCF 或 IIS
  • 我想在 2-4 名开发人员(聪明的学生)团队中开发它。

那么,在 C# .Net 中创建这个可以吗?或者我不会浪费时间在它可以为开发人员提供的轻松承诺上,并转向 C\C++?

那么就我而言,用 C# 编写服务器应用程序是否合理?


Offtop - 为什么不 WCF

警告:这里变得主观。

当您的大公司每一次服务会话的数据交换相对较少时,WCF 会非常有用。

当你有视频、现场视频时,一切都会变得复杂。大量数据、大量用户同时从您的服务中流入和流出。

尝试通过http绑定进行实时视频流 - 比尝试其他人你会明白为什么我不喜欢使用WCF进行实时流媒体的想法 - 它很慢,实时流信息不需要way2much,毕竟你曾经有过见过 WCF 上的实时视频流应用程序吗?不 - 你还没有 - 可能你已经看过 +- Silverlight + IIS 对上的实时视频,我不喜欢它,因为它仅适用于 Silverlight\WindowsMediaPlayer 视频流解决方案,而我想要的不止于此。

我喜欢拥有具有 UI 的跨平台客户端。而且我不喜欢(这都是我个人的意见 - 所以它是主观的)Silverlight+IIS+WCF 组。那么我该怎么办 - 正确使用套接字,以 FLV 和 Flash 等古老而简单的格式进行流作为后端客户端 - 某些部分的开发更简单,通过网络进行实时视频的方式比从 MS 获得的方式更保守今天。

我喜欢 Flash FLV 实时流媒体,因为您只需打开套接字并开始向其发送实时 FLV 视频数据(对于每个用户 FLV 标头和 FLV“标签”,一一:视频标签、音频标签、视频标签、音频标签等) Flash 就可以播放它!没有特殊\不寻常的代码。它快速、易于支持,并且不会使客户需要任何新的\不寻常的东西。您在服务器端可以充分利用视频\音频数据表示的“TAG”形式。

简而言之,这就是为什么我不想使用 WCF - 很难在客户端播放实时视频,对实时视频服务器没有一般好处。

当大多数实时数据通过套接字传输时,为什么还要费心使用 WCF 进行服务管理。

在 2009 年下半年和 2010 年上半年,我开始接触 WCF、实时视频流、silverlight 和 flash,比较客户端/服务器创建过程,与一群谨慎有趣的开发人员一起阅读不同的格式。一般来说,在项目结束时,我们有很多迷你服务器流式传输实时数据,并且有很多不同的客户端接收数据。比较我们所做的一切,我们得出的结论与我在这里向您介绍的结论很接近。

这就是为什么我不想在最近的项目中使用 WCF - 我不想考虑如何传递媒体数据,我想专注于它的过滤\编辑。


为什么出现这个问题

我们开始在 C 中使用 FFmpeg\OpenCV,并且使用它们来操作数据非常简单...在 C 中...在 Linux 上...

但是当我们开始使用 .Net 绑定时(我们现在正在使用Tao.FFmpeg)我们发现,在大多数情况下,我们最终会经常使用C# Marshal,并且有2个变量用于其C模拟(指针问题)等等。我希望我们不会在 Emgu CV 上看到这样的问题,但钢铁这让我有点害怕......

  • I want it to work on windows servers.
  • It will be a cloud type server - it'll consist of modules\parts running on different machines all over the world using http\tcp + upnp to connect to each other
  • There are going to be controlling\monitoring\observing modules on each machine to provide stats on performance
  • This net is going to be working with large amount of VIDEO\AUDIO life streaming\broadcasting data
  • It is going to use FFMPEG for re-encoding and OpenGL, OpenCV and such for filtering (.NET wrappers exist and work BTW)
  • It will not use any WCF or IIS
  • I want to develop it in team of 2-4 developers, smart students.

So is it OK to create this in C# .Net or I shall not waste my time on promises of ease it could provide to a developer and go C\C++?

So is it reasonable to write a server application in C# in my case?


Offtop - why not WCF

Warning: it gets way to subjective in here.

WCF is grate when you have big corp with relatively small data exchange per one session of service.

When you have video, LIVE video, it all gets complicated. Large amounts of data, lots of users stream in and out from your service at the same time.

Try to do live video streaming over http binding - than try it with others than you'll see why I do not like idea of live streaming with WCF - it is slow, with way2much not needed for live streaming info and after all have you ever seen a live video streaming app on WCF? No - you haven't - may be you have seen +- live video on Silverlight + IIS pair which I do not like because it is just for Silverlight\WindowsMediaPlayer video streaming solution while I want more than that.

I love to have cross-platform clients with reach UI’s. And I do not like (it is all here my personal opinion - so it is subjective) Silverlight+IIS+WCF group. So what shall I do - right go to sockets, streams in such old and simple formats like FLV and Flash as back end client - Simpler in development in some parts, more conservative way of doing live video over the web than one you get from MS today.

I love Flash FLV live streaming because you just open socket and start sending live FLV video data onto it (for each user FLV header and than FLV "TAG's", one by one: video tag, audio tag, video tag, audio tag etc) and Flash plays it! With no special\unusual code. It is fast, easy in supporting, and does not make client need anything new\unusual. And you on server side can take grate use of that "TAG" form of video\audio data representation.

So that is in short why I just do not want to use WCF - hard to get live video playing out from it on client side, no general benefits for live video server.

And when most of live data goes thru sockets why to bother with using WCF for service management.

During last half of 2009 and first half of 2010 I was getting into WCF, live video streaming, silverlight and flash, comparing process of client\server creation, reading different formats with a team of wary interesting developers. In general at the end of project we had lots of mini servers streaming live data and lots of different clients receiving it. Comparing all we've done we came to conclusions which are near one I present you here.

That is why I do not want to use WCF in my nearest project - I do not want to think about how to deliver media data, I want to focus on its filtering\editing.


Why the question appeared

We started playing with FFmpeg\OpenCV in C, and it is pretty simple to manipulate data using them... in C... on Linux...

But when we started to play with there .Net bindings (we are now playing with Tao.FFmpeg) we found that in most cases we end up playing with C# Marshal a lot, and having 2 variables for its C analog (problem of pointers) and so on. I hope we will not see such problem with Emgu CV but steel it makes me a little bit afraid...

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

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

发布评论

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

评论(5

回眸一笑 2024-09-18 00:18:48

我认为这是完全合理的。 C# 在易于开发方面的优势将大大超过不使用 C++ 带来的任何性能缺陷。

C# 通常比 C++ 更具跨平台性。诚然,C++ 是一种跨平台语言,但 C++ 程序用于与系统交互的 API 之间存在很大差异。 C# 和 .Net/Mono 对套接字层有更加标准化的接口。

最后,对于像这样雄心勃勃的项目,将项目变成可用的形式是比获得尽可能高的性能更重要的目标。仅当项目完成时,性能才重要。用 C# 编写它,因为这会给你最大的完成机会。然后担心性能。

I think it's entirely reasonable. The benefits of C# with regard to ease of development will greatly outweigh any performance drawbacks of not using C++.

C# is generally more cross-platform than C++. True, C++ is a cross-platform language, but there are large differences between the APIs that C++ programs use to interact with the system. C# and .Net/Mono have a much more standardized interface to the socket layer.

Finally, with ambitious projects like this, getting the project into a usable form is a much more important goal than getting the highest performance possible. Performance only matters if the project is complete. Write it in C# because that will give you the greatest odds of completion. Then worry about performance.

染火枫林 2024-09-18 00:18:48

我不太清楚为什么人们提出跨平台问题,因为 OP 明确表示该应用程序将在 Windows 上运行。

至于实际问题。

  1. 您能否用 C# 构建一个通过 tcp/http 进行通信且不必在 IIS 中运行的服务器应用程序。 ->是的。
  2. 您能否用 C# 构建一个高性能且可扩展的服务器应用程序 ->是的。
  3. 你能和学生一起这样做吗->或许。取决于学生...;)但这与使用的语言无关。

这是我会做的事吗?是的。我们已经做到了。目前,我们的 ac# 应用程序在大约 20,000 台机器上运行,这些机器通过 TCP 进行有效通信。我们没有使用 WCF,但我们确实决定使用基于 http 的 RESTful 风格服务来进行数据传输。

我们最大的问题是简单地调整应用程序以一次通过线路传输“正确”的数据量。该网络用于数据收集和存储。平均每天收集约 200GB 的数据。

更新
我想澄清一下上述应用程序。上述安装中的 20,000 台计算机是客户端(XP、Vista、7、2003 Server 和 2008 Server)。其中只有一台数据收集点服务器。当连接到网络时,客户端每 45 秒向服务器发送一次数据。大约 97% 的机器以这种方式保持连接,其余的机器每周连接几次。

这相当于服务器每天处理大约 3700 万个请求。

现在,可以肯定的是,每个请求相对较小,约为 5KB 到 6KB。然而,大量的请求表明 C# 应用程序可以处理这些连接的管理,这是 OP 问题的主要部分。

因为 OP 的文件很大(视频),所以真正的问题只是数据传输。这将更多地受到硬盘速度、网络速度和延迟的阻碍。这些问题与您使用哪种语言无关,并且会根据可用带宽限制每台服务器的连接数量。

为了解决这个问题,我们将其限制为一台服务器作为示例。如果您的视频速率为 400kb/s,并且有 25MB 的互联网连接,那么该设备实际上只能处理大约 62 个同时连接。这远远低于我们应用程序正在执行的连接数,因此存在舍入误差。

假设完美的网络条件(这并不存在),将互联网连接增加到 100MB(这可能很昂贵)意味着同时连接数增加 4 倍,达到 240 个;仍然完全可以管理。

然而,网络只是等式的一方面。服务器上的驱动速度非常重要。您最好拥有一个能够连续传输这么多数据的良好磁盘阵列。我知道驱动器声称可以传输 3GB 数据,但从未制造过能够使通道饱和的驱动器。这意味着在服务器设置方面需要认真规划和投入资金。

所有这一切的目的是说,在你的情况下,语言一点也不重要。您还有其他更大的争用问题。在这种情况下,请选择能够帮助您更快完成项目的语言。

I'm not exactly sure why people have brought up Cross Platform concerns as clearly the OP has stated the app will run on Windows.

As to the actual questions.

  1. Can you build a server application that communicates via tcp/http in C# that does not have to run in IIS. -> Yes.
  2. Can you build a server application that is performant and scales in C# -> Yes.
  3. Can you do so with Students -> Maybe. Depends on the students... ;) But that is irrespective of the language in use.

Is this something I would do? Yes. We've done that. We have a c# app running on approximately 20,000 machines right now that are communicating effectively over tcp. We aren't using WCF, but we did decide to use RESTful style services over http for the data transfer.

Our biggest issue was simply tuning the app to transfer the "right" amount of data over the wire at a time. This network is for data collection and storage. It's averaging around 200GB of data collected a day..

UPDATE
I wanted to clarify a bit about the above app. The 20,000 machines at the above installation are clients (XP, Vista, 7, 2003 Server, and 2008 Servers). There's only one data collection point server in the mix. The clients post data to the server, when connected to a network, once every 45 seconds. Roughly 97% of the machines stay connected in this manner, the rest connect a couple times a week.

This works out to the server processing about 37 million requests a day.

Now, to be sure, each request is relatively small at around 5KB to 6KB each. However, the shear number of requests shows that a C# application can handle managing those connections, which is the bigger part of the OP's problem.

Because the OP's files are large (Video), then the real issue is simply in data transfer. Which will be hindered more by hard drive speeds, as well as network speed and latency. Those issues are irrespective of which language you are working in and will limit the number of connections per server based on available bandwidth.

Working this out let's limit it down to one server for an example. If you have a video rate of 400kb/s then and a 25MB connection to the internet, then that box could physically only handle around 62 simultaneous connections. Which is so FAR below the number of connections our app is doing as to be a rounding error.

Assuming perfect network conditions (which don't exist), pumping that internet connection up to 100MB (which can be expensive) means a 4x increase in simultaneous connections to 240; still completely manageable.

However, the network is only one side of the equation. Drive speed on the servers matters a lot. You better have a good disk array capable of continuously delivering that amount of data. I know drives claim 3GB data transfers, but a drive which can saturate the channel has never been built. Which means serious planning and money in the server setup.

The point of all of this is to say that the language doesn't matter one bit in your situation. You have other much larger contention issues. With that being the case, go with the language that will help you get the project done faster.

拥有 2024-09-18 00:18:48

为什么要停在 C# 上,如果你(可能)想要跨平台,用 Python 或类似的语言编写,你会发现脚本语言的网络方面比 C# 好得多(因为这几乎是脚本语言的角色)到现在,运行基于网络的服务器)。

您会发现开发人员的工作效率比 C# 提高了很多(就像 C# 比 C++ 的工作效率更高一样),并且有很多人了解并希望在这些系统上工作。听起来服务器本身的性能不如网络重要,因此脚本似乎是您的最佳选择。另外,与 C# 相比,ffmpeg 库使用 pyffmpeg 与 python 的集成更加紧密(嗯,主要是)。

而且它会更酷、更有趣,而且非常跨平台!

Why stop at C#, if you (possibly) want cross-platform, write it in Python or similar, you'll find that the networking aspects of a scripting language are far better than C# (as that's pretty much the role scripting languages are put to nowadays, running web-based servers).

You'll find developer productivity is much improved over C# (just as C# has better productivity over C++), and there are lots of people who know and want to work on these systems. It sounds like performance of the servers themselves is of less importance than the networking, so it appears that script would be your best choice. Plus ffmpeg libraries are more tightly integrated with python using pyffmpeg than C# (well, mostly).

And it'd be a lot cooler, more fun, and very much cross-platform!

感情废物 2024-09-18 00:18:48

如果您想要 C# 以及跨平台能力,您的开发将必须针对 Mono 平台(或其他跨平台 .NET 运行时,如果您能找到的话)。您可能不得不放弃 VisualStudio,也许还有一些 Microsoft 特定的库和工具,但您仍然可以在多个平台上使用 C#。只需确保在此过程中尽早开始多平台构建和测试,否则稍后更改将是地狱。

If you want C# and also cross-platform abilities, your development will have to target the Mono platform (or another cross-platform .NET runtime, if you can find one). You might have to give up VisualStudio, and maybe some Microsoft-specific libraries and tools, but you can still have C# on multiple platforms. Just make sure you start the multi-platform building and testing EARLY in the process or it will be hell to change things later.

尘曦 2024-09-18 00:18:48

如果应用程序的目标是仅在 Windows 平台上运行,我完全确定用 C# 编写此应用程序。许多类似的应用程序现在可以运行,但我们甚至不知道。

如果目标是在多个平台上运行,您应该首先封装非 Windows 平台可能给您的应用程序带来的所有问题。

如果在这种情况下,C# 能够完成 C++ 所做的一切,为什么必须用 C++ 编写它呢?我会使用 C++ 对硬件级的东西进行编程,比如机器人或其他东西。要编写服务器应用程序,C# 非常适合您的需求,它就是为这些事情而设计的。

而且C#是跨平台的,你只需要合适的工具就可以让它在特定的平台上运行。

If the target of the application is to run only on Windows platforms, I'm completely sure to write this application in C#. Many applications like that can be running right now and we don't even know that.

If the target is to run on multiple platformms, you should encapsulate first all the problems that a non-windows platform can bring to your application.

Why do you have to write it in C++ if, in this case, C# is capable to do everything that C++ does? I would use C++ to program things on hardware-level things, like a robot or something else. To write a server application, C# will fit very well what you want, it was designed for these things.

And C# is cross-platform, you just need the right tool to make it work on a specific platform.

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