windows 应用程序之间的通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5181419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Communication between applications
提问by Zlobaton
I have 3 choices to use : sockets, activeX, com , in order to communicate between applications on one computer. Which is faster ?
我有 3 种选择可以使用:sockets、activeX、com,以便在一台计算机上的应用程序之间进行通信。哪个更快?
回答by Hans Passant
As long as this runs on one machine, interprocess communication is fundamentally throttled by the bus bandwidth. A memory-to-memory copy, whether that's done in the TCP/IP stack, the named pipe support code or shared memory. Which makes them all equally efficient.
只要它在一台机器上运行,进程间通信从根本上受到总线带宽的限制。内存到内存的复制,无论是在 TCP/IP 堆栈、命名管道支持代码还是共享内存中完成的。这使得它们都同样高效。
One detail matters though, the amount of data that's transferred and the number of software layers you travel through to get the job done. The memory bus bandwidth only throttles when the amount of data is large. That isn't necessarily the case for a remote procedure call protocol like COM. Only the arguments of the function call needs to be serialized, that could be only a handful of bytes if you don't pass arrays. Now the overhead starts to matter, there's a fair amount of it when you use a high-level protocol like COM.
但有一个细节很重要,即传输的数据量以及完成工作所需经过的软件层数。内存总线带宽仅在数据量较大时进行节流。对于像 COM 这样的远程过程调用协议,情况不一定如此。只有函数调用的参数需要序列化,如果不传递数组,它可能只有少数字节。现在开销开始变得重要,当您使用像 COM 这样的高级协议时,开销会很大。
The obvious disadvantage of using sockets is that you'll have to write all the de/serialization code yourself. Nontrivial if the protocol with the component isn't simple. Trading your working hours for convenience is the typical choice, only you can make it.
使用套接字的明显缺点是您必须自己编写所有反/序列化代码。如果与组件的协议不简单,则意义重大。用你的工作时间换取方便是典型的选择,只有你能做到。
回答by Hans Passant
Well, think about it - socket is the lowest level, COM is using sockets, ActiveX is using COM. So which one is faster? Of course, sockets. But that is only if you are asking about program execution speed and data transfer rates. Developing programs using sockets, however, can be much harder if you don't know what you are doing. Not to mention that you possibly can come up with some bad implementation that will be worse than COM. Also, there are not that much of reusable components you can get for sockets as you get using ActiveX, not to mention that if you want to communicate with MS Office, you will have to use COM.
好吧,想一想——socket是最底层的,COM用的是sockets,ActiveX用的是COM。那么哪个更快呢?当然是插座。但这仅当您询问程序执行速度和数据传输速率时。但是,如果您不知道自己在做什么,则使用套接字开发程序会困难得多。更不用说您可能会提出一些比 COM 更糟糕的糟糕实现。此外,在使用 ActiveX 时,您可以为套接字获得的可重用组件并不多,更不用说如果您想与 MS Office 通信,您将不得不使用 COM。
回答by Nico
You don't give much detail about what you're trying to do or what considerations you need to make. For example, by "fast" do you mean high bandwidth? Low latency? Is there a possibility you might need to communicate across computers later? Etc.
您没有详细说明您要做什么或需要考虑什么。例如,“快”是指高带宽吗?低延迟?以后是否有可能需要跨计算机进行通信?等等。
That said, ActiveX is a special case of COM (introduction to activeX). If you're familiar with COM or ActiveX already, and depending on what exactly you're trying to do, you may be able to get away with writing relatively little code because MS dev tools can handle a lot of it for you.
也就是说,ActiveX 是 COM 的一个特例(ActiveX 介绍)。如果您已经熟悉 COM 或 ActiveX,并且取决于您究竟要做什么,您可能能够避免编写相对较少的代码,因为 MS 开发工具可以为您处理很多。
If you're not familiar with it though, it's a fairly complex technology that may be tricky to wrap your head around. So if you're just trying to implement some basic inter-process communication it may be easier to go with sockets. On the other hand, that may require more low level work on your part.
不过,如果您不熟悉它,那么它是一项相当复杂的技术,可能很难理解。因此,如果您只是想实现一些基本的进程间通信,那么使用套接字可能会更容易。另一方面,这可能需要您进行更多的低级别工作。
回答by Simon Mourier
ActiveX is more or less a fancy marketing name on COM (an ActiveX component / control is an object that just supports the IUnknown interface), so your choice is really down to COM vs Socket.
ActiveX 或多或少是 COM 上的一个花哨的营销名称(ActiveX 组件/控件是一个仅支持 IUnknown 接口的对象),因此您的选择实际上取决于 COM 与 Socket。
For cross process communication, you canwrite something faster with sockets... if you're a good socket and Windows programmer, because you will be on your own, as Sockets will basically do nothing to help you. That does not mean COM is not fast, or has bad performance, but you always theoretically can do better than an out-of-the-box system, but only if you master the whole thing.
对于跨进程通信,您可以使用套接字更快地编写一些东西......如果您是一个优秀的套接字和 Windows 程序员,因为您将独自一人,因为套接字基本上不会帮助您。这并不意味着 COM 不快,或性能不佳,但理论上您总是可以比开箱即用的系统做得更好,但前提是您掌握了整个系统。
On last thing, if you need to communicate with 3rd party products or other platforms than Windows, Sockets are more portable.
最后一点,如果您需要与 3rd 方产品或 Windows 以外的其他平台进行通信,则 Sockets 更具可移植性。
回答by count0
I would also delegate the IPC to a framework e.g. ACE (adaptive communication framework). Ace's implementation for example, is stable and it's cross platform.
我还会将 IPC 委托给一个框架,例如 ACE(自适应通信框架)。例如,Ace 的实现是稳定的并且是跨平台的。
回答by David Heffernan
It may not actually matter which is faster. If so then choose the method that will be the most convenient to develop and maintain. It may be possible to save your own time even if you can't save any measurable runtime.
哪个更快实际上可能并不重要。如果是这样,则选择最便于开发和维护的方法。即使您无法保存任何可测量的运行时间,也可以节省您自己的时间。
回答by Steve
It's hard to say which is faster, but using COM is certainly the most flexible of the choices you listed. Unless you like grovelling through byte streams.
很难说哪个更快,但使用 COM 肯定是您列出的选择中最灵活的。除非你喜欢浏览字节流。