本文介绍了管道和插座之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了两个答案,但它们似乎与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打包通信;该通信可以扩展到本地主机之外.请注意,套接字的不同端点可以共享相同的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() and write() to a pipe.
      • when you want to connect the output of one process to the input of another process... see popen()

      顺便说一句,您可以使用 netcat 或将套接字连接到管道.

      BTW, you can use netcat or socat to join a socket to a pipe.

      这篇关于管道和插座之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 19:56