本文介绍了管道和套接字有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找到了几个答案,但它们似乎与 Windows 机器特别相关.所以我的问题是管道和套接字之间有什么区别,你应该什么时候/如何选择一个?
I found a couple of answers, but they seem to be specifically relating to Windows machines.So my question is what are the differences between pipes and sockets, and when/how should you choose one over the other?
推荐答案
管道和套接字都处理字节流,但它们的处理方式不同......
Both pipes and sockets handle byte streams, but they do it in different ways...
- 管道仅存在于特定主机中,它们指的是虚拟文件之间的缓冲,或连接该主机内进程的输出/输入.管道内没有数据包的概念.
- 套接字使用 IPv4 或 IPv6 将通信打包;该通信可以扩展到本地主机之外.注意一个socket的不同端点可以共享同一个IP地址;但是,它们必须侦听不同的 TCP/UDP 端口才能这样做.
用法:
- 使用管道:
- 当您想在特定服务器中将数据作为文件读取/写入时.如果您使用的是 C,您可以
read()
和write()
到管道. - 当您想将一个进程的输出连接到另一个进程的输入时...参见 popen()
- Use pipes:
- when you want to read / write data as a file within a specific server. If you're using C, you
read()
andwrite()
to a pipe. - when you want to connect the output of one process to the input of another process... see popen()
顺便说一句,您可以使用 netcat 或 socat 将套接字连接到管道.
BTW, you can use netcat or socat to join a socket to a pipe.
这篇关于管道和套接字有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- when you want to read / write data as a file within a specific server. If you're using C, you
- 当您想在特定服务器中将数据作为文件读取/写入时.如果您使用的是 C,您可以