Named pipe vs socket

Named pipe vs socket

Named pipe vs socket



管道僅存在於特定主機內,它們指的是虛擬文件之間的緩衝,或連接該主機內進程的輸出/輸入。 管道內沒有數據包的概念。

    套接字使用 IPv4 或 IPv6 對通信進行分組; 這種通信可以擴展到本地主機之外。 請注意,套接字的不同端點可以共享相同的 IP 地址; 但是,它們必須偵聽不同的 TCP/UDP 端口才能這樣做。

    當您想在特定服務器中將數據作為文件讀取/寫入時。 如果您使用的是 C,您可以將 read() 和 write() 寫入管道。

    當您想將一個進程的輸出連接到另一個進程的輸入時…請參閱 popen()

使用套接字在不同的 IPv4 / IPv6 端點之間發送數據。 很多時候,這發生在不同的主機之間,但套接字可以在同一主機內使用

順便說一句,您可以使用 netcat 或 socat 將套接字連接到管道。


Both pipes and sockets handle byte streams, but they do it in different ways...
	pipes only exist within a specific host, and they refer to buffering between virtual files, or connecting the output / input of processes within that host. There are no concepts of packets within pipes.
	sockets packetize communication using IPv4 or IPv6; that communication can extend beyond localhost. Note that different endpoints of a socket can share the same IP address; however, they must listen on different TCP / UDP ports to do so.
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()	

Use sockets to send data between different IPv4 / IPv6 endpoints. Very often, this happens between different hosts, but sockets could be used within the same host	

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

2 thoughts on “Named pipe vs socket


    在 Windows 和 POSIX 系統上,命名管道都提供了一種在同一台機器上運行的進程之間進行進程間通信的方式。命名管道為您提供的是一種發送數據的方法,而不會因涉及網絡堆棧而降低性能。

    就像您有一個服務器監聽傳入請求的 IP 地址/端口一樣,服務器也可以設置一個可以監聽請求的命名管道。在任何一種情況下,客戶端進程(或 DB 訪問庫)都必須知道發送請求的具體地址(或管道名稱)。通常,存在一個常用的標準默認值(很像 HTTP 的端口 80,SQL 服務器在 TCP/IP 中使用端口 1433;\\.\pipe\sql\query 用於命名管道)。

    通過設置額外的命名管道,您可以運行多個 DB 服務器,每個服務器都有自己的請求偵聽器。


    — 順便說一句,在 Windows 世界中,您也可以將命名管道連接到遠程機器 — 但在這種情況下,命名管道是通過 TCP/IP 傳輸的,因此您將失去性能。使用命名管道進行本地機器通信。

    Both on Windows and POSIX systems, named-pipes provide a way for inter-process communication to occur among processes running on the same machine. What named pipes give you is a way to send your data without having the performance penalty of involving the network stack.

    Just like you have a server listening to a IP address/port for incoming requests, a server can also set up a named pipe which can listen for requests. In either cases, the client process (or the DB access library) must know the specific address (or pipe name) to send the request. Often, a commonly used standard default exists (much like port 80 for HTTP, SQL server uses port 1433 in TCP/IP; \\.\pipe\sql\query for a named pipe).

    By setting up additional named pipes, you can have multiple DB servers running, each with its own request listeners.

    The advantage of named pipes is that it is usually much faster, and frees up network stack resources.

    — BTW, in the Windows world, you can also have named pipes to remote machines — but in that case, the named pipe is transported over TCP/IP, so you will lose performance. Use named pipes for local machine communication.


    IPC performance: Named Pipe vs Socket

    每個人似乎都說命名管道比套接字 IPC 更快。它們的速度有多快?我更喜歡使用套接字,因為它們可以進行雙向通信並且非常靈活,但是如果數量很大,則會選擇速度而不是靈活性。

    Everyone seems to say named pipes are faster than sockets IPC. How much faster are they? I would prefer to use sockets because they can do two-way communication and are very flexible but will choose speed over flexibility if it is by considerable amount.


你的電子郵件位址並不會被公開。 必要欄位標記為 *