windows 管道和套接字有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3518605/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:04:03  来源:igfitidea点击:

What's the difference between pipe and socket?

c++windows

提问by wamp

Both can be used for communicating between different processes,

两者都可以用于不同进程之间的通信,

what's the difference?

有什么不同?

采纳答案by Jerry Coffin

Windows has two kinds of pipes: anonymous pipes and named pipes. Anonymous pipes correspond (fairly) closely to Unix pipes -- typical usage is for a parent process to set them up to be inherited by a child process, often connected to the standard input, output and/or error streams of the child. At one time, anonymous pipes were implemented completely differently from named pipes so they didn't (for one example) support overlapped I/O. Since then, that's changed so an anonymous pipe is basically just a named pipe with a name you don't know, so you can't open it by name, but it still has all the other features of a named pipe (such as the aforementioned overlapped I/O capability).

Windows 有两种管道:匿名管道和命名管道。匿名管道(相当)接近于 Unix 管道——典型用法是父进程将它们设置为由子进程继承,通常连接到子进程的标准输入、输出和/或错误流。曾几何时,匿名管道的实现与命名管道完全不同,因此它们(例如)不支持重叠 I/O。从那以后,情况发生了变化,因此匿名管道基本上只是一个您不知道名称的命名管道,因此您无法按名称打开它,但它仍然具有命名管道的所有其他功能(例如前面提到的重叠 I/O 能力)。

Windows named pipes are much more like sockets. They originated with OS/2, where they were originally the primary mechanism for creating client/server applications. They were originally built around NetBIOS (i.e., used NetBIOS both for addressing and transport). They're tightly integrated with things like Windows authentication, so you can (for example) have a named pipe server impersonate the client to restrict the server to doing things the client would be able to do if logged in directly. More recently, MS has gone to some trouble to get rid of the dependence on NetBIOS, but even though they can now use IP as their transport (and DNS for addressing, IIRC) they're still used primarily for Windows machines. The primary use on other machines is to imitate Windows, such as by running Samba.

Windows 命名管道更像套接字。它们起源于 OS/2,在那里它们最初是创建客户端/服务器应用程序的主要机制。它们最初是围绕 NetBIOS 构建的(即,使用 NetBIOS 进行寻址和传输)。它们与 Windows 身份验证等内容紧密集成,因此您可以(例如)让命名管道服务器模拟客户端,以限制服务器执行客户端直接登录时可以执行的操作。最近,MS 在摆脱对 NetBIOS 的依赖方面遇到了一些麻烦,但即使他们现在可以使用 IP 作为传输方式(以及用于寻址的 DNS,IIRC),但它们仍然主要用于 Windows 机器。在其他机器上的主要用途是模仿 Windows,例如通过运行 Samba。

回答by Will A

(Shamelessly cribbed from http://www.perlmonks.org/?node_id=180842)

(无耻地抄袭自http://www.perlmonks.org/?node_id=180842

Pipes are fast and reliable, because they are implemented in memory on a single host where both communicating processes run. Sockets are slower and less reliable, but are much more flexible since they allow communication between processes on different hosts.

管道既快速又可靠,因为它们是在运行两个通信进程的单个主机上的内存中实现的。套接字较慢且不太可靠,但更加灵活,因为它们允许不同主机上的进程之间进行通信。

回答by bioffe

Sockets would use some sort of IP protocol like TCP/IP or UDP, thus would be slower, but your code'd be more portable if you'd need to communicate over a network. There is a third Shared mem approach and forth Mach ports (in this case I am not sure about it would work with Windows )

套接字将使用某种 IP 协议,如 TCP/IP 或 UDP,因此速度会较慢,但如果您需要通过网络进行通信,则您的代码将更具可移植性。还有第三种 Shared mem 方法和第四个 Mach 端口(在这种情况下,我不确定它是否适用于 Windows)

回答by VoodooChild

(Off the top of my head)

(离开我的头顶)

Pipe: A tube with a small bowl at one end; used for smoking tobacco

管子:一端有一个小碗的管子;用于吸烟

Socket: Receptacle where something (a pipe, probe or end of a bone) is inserted

插座:插入某物(管子、探针或骨头末端)的插座

Anyways:

无论如何:

"A major difference between pipes and sockets is that pipes require a common parent process to set up the communications channel. A connection between sockets can be set up by two unrelated processes, possibly residing on different machines."

“管道和套接字之间的主要区别在于,管道需要一个共同的父进程来设置通信通道。套接字之间的连接可以由两个不相关的进程建立,可能驻留在不同的机器上。”

回答by Pickbothmanlol

They both do the same function, the only difference is that pipes are more efficient as they are closest one can get to the barebones of internets. Sockets are an abstraction built on top of series of tubes (pipes) as a result they are slower (just as java is slower than native assembly code).

它们都执行相同的功能,唯一的区别是管道效率更高,因为它们最接近互联网的准系统。套接字是建立在一系列管道(管道)之上的抽象,因此它们更慢(就像 java 比本地汇编代码慢一样)。