What Is a Communication Pipe?

Pipeline communication (Communication Pipeline) means that the sending process sends a large amount of data into the pipeline in the form of character streams, and the receiving process can receive data from the pipeline, and the two use the pipeline to communicate. Whether it is a SQL Server user or a PB user, as a C / S structure development environment, they have a common method in the implementation of network communication-named pipes. Due to the non-unique nature of the current operating system, each system has its own communication protocol, which causes difficulties in communication between different systems. Although the TCP / IP protocol has now developed into the Internet standard, it still cannot guarantee the smooth progress of C / S applications. As a communication method, named pipes have its unique advantages. This is mainly reflected in that it does not depend entirely on a certain protocol, but is applicable to any protocol-as long as communication can be achieved.

Pipeline communication (Communication Pipeline) means that the sending process sends a large amount of data into the pipeline in the form of character streams, and the receiving process can receive data from the pipeline, and the two use the pipeline to communicate. Whether it is a SQL Server user or a PB user, as a C / S structure development environment, they have a common method in the implementation of network communication-named pipes. Due to the non-unique nature of the current operating system, each system has its own communication protocol, which causes difficulties in communication between different systems. Although the TCP / IP protocol has now developed into the Internet standard, it still cannot guarantee the smooth progress of C / S applications. As a communication method, named pipes have its unique advantages. This is mainly reflected in that it does not depend entirely on a certain protocol, but is applicable to any protocol-as long as communication can be achieved.
Chinese name
Pipeline communication
Foreign name
Communication Pipeline

Features of Pipeline Communication

Use flexibility
Named pipes have great flexibility in use, as shown in:
1) Can be used for both local and network.
2) Can be referenced by its name.
3) Support multiple client connections.
4) Support two-way communication.
5) Support asynchronous overlap I / O operation.
However, only Windows NT currently supports server-side named pipe technology. [1]

Technical requirements for pipeline communication

Implementation of named pipe programming
1. The implementation process of communication between the named pipe Server and Client
(1) Establish a connection: The server creates an instance of a named pipe through the function CreateNamedPipe and returns a handle for future operations, or creates a new instance for an existing pipe. If an instance pipeline is available before the defined timeout value becomes zero, the pipeline is successfully created and a pipeline handle is returned to listen for connection requests from the client. This function is implemented by the ConnectNamedPipe function.
On the other hand, the client uses the function WaitNamedPipe to make the service process wait for an instance connection from the client. If a pipe can be used for the connection before the timeout value becomes zero, WaitNamedPipe will return True and call by calling CreateFile or CallNamedPipe Connection to the server. At this point, the server will accept the client's connection request, and the connection is successfully established. The server ConnectNamedPipe returns True, and the client CreateFile will return a handle to the pipe file.
In terms of timing, first, the client successfully creates an instance of CreateFile in the server within a limited time through WaitNamedPipe, and then the two parties successfully connect through ConnectNamedPipe and CreateFile, and return a file handle for communication. At this time, both parties can communicate.
(2) Communication: After the connection is established, the client and server can use ReadFile and WriteFile to use the obtained pipe file handle to exchange information with each other.
(3) Termination of the connection: When the communication between the client and the server ends, or when one party needs to disconnect for some reason, the client should call CloseFile, and the server should then call DisconnectNamedPipe. Of course, the server can also terminate the connection by unilaterally calling DisconnectNamedPipe. Finally, the function CloseHandle should be called to close the pipeline.
Named pipe server and client code implementation
Client
HANDLE CltHandle;
char pipenamestr [30];
sprintf (pipenamestr, "\\\\ servername \\ pipe \\ pipename")
if (WaitNamedPipe (pipenamestr, NMPWAITWAITFOREVER) == FALSE
// The pipe name must follow UNC, the format is \ \. \ pipe \ pipname, names are not case sensitive.
AfxMessageBox ("The operation failed, please make sure that the server establishes the pipeline instance correctly!");
Else
CltHandle = CreateFile (pipenamestr, GENERICREAD | GENERICWRITE, FILESHAREREAD | FILESHAREWRITE, NULL, OPENEXISTING,
// In order to connect with a named pipe, this parameter should always be OPENEXISTING
FILEATTRIBUTEARCHIVE | FILEFLAGWRITETHROUGH,
// FILE_FLAG_WRITE_THROUGH will make the pipeline WriteFile call block until the data transfer is successful.
NULL);
If (CltHandle == INVALID_HANDLE_VALUE)
AfxMessageBox ("pipe connection failed");
Else
DoUsertTransactInfo ();
// Execute user-defined information exchange function-read and write information from the pipeline.
...
Server
HANDLE SvrHandle;
char pipenamestr [30];
sprintf (pipenamestr, "\\\\. \\ pipe \\ pipename")
SvrHandle = CreateNamedPipe (pipenamestr,
PIPEACCESSDUPLEX | FILEFLAGWRITETHROUGH,
// Blocking mode, this mode is only valid for "byte transmission pipeline" operation.
FILEWAIT | PIPETYPEBYTE,
// Byte mode
PIPEUNLIMITEDINSTANCES,
128,128,
NULL, NULL);
// SECURITYATTRIBUTES structure pointer, describes a new pipe, determines the inheritance rights of the child process, if NULL, the named pipe cannot be inherited.
If (SvrHandle == INVALID_HANDLE_VALUE)
AfxMessageBox ("The pipeline creation failed, please make sure the client provides a connection possible!");
Else
If (ConnectNamedPipe (SvrHandle, NULL) == FALSE)
AfxMessageBox ("Failed to establish a connection!");
Else
DoUsertTransactInfo ();
// User-defined information exchange function
...

Pipeline communication considerations

Program design considerations
1. If the named pipe client is open, the function will force the pipe to be closed. If the pipe is closed with DisconnectNamedPipe, its client must also use CloseHandle to close the last pipe.
2. The hFile handles of ReadFile and WriteFile are returned by CreateFile and ConnectNamedPipe.
3. A pipe handle that has been connected by a client before the client establishes a connection through ConnectNamedPipe, the server must use the DisconnectNamedPipe function to forcibly detach the existing connection. Detaching the pipeline on the server will cause data loss in the pipeline. Use the FlushFileBuffers function to ensure that the data is not lost.
4. The named pipe server can use the ConnectNamedPipe function through a newly created pipe handle or a pipe handle that has been connected to other clients, but before connecting the new client, the server must use the function DisconnectNamedPipe to cut off the previous client handle, otherwise ConnectNamedPipe will return False.
5. Blocking mode, this mode is only valid for "byte transfer pipe" operation, and requires that the client and server are not on the same machine. If this mode is used, the function can only return effectively if the function successfully writes data to the remote computer pipe buffer over the network. If this mode is not used, the system will run the default mode to improve the efficiency of the network.
6. The user must use FILE_CREATE_PIPE_INSTANCE to access the named pipe object. After a new named pipe is established, the access control list from the security parameters defines the permissions to access the named pipe. All named pipe instances must use parameters such as a unified pipe transfer mode and pipe mode. The client is not started, and the pipe server cannot perform a blocking read operation, otherwise an empty blocking state will occur. When the last handle of the last named pipe instance is closed, the named pipe should be deleted.

Other ways of pipe communication

About pipe communication in Unix
Starting from Unix System V, the system provides two data communication methods, named pipes and unnamed pipes.
An unnamed pipe provides a communication channel that transmits information in a bitstream manner to the process and child processes that establish the pipe. Logically, it can be regarded as a pipe file, which is physically composed of a high-speed buffer of the file system, and rarely uses peripherals. The sending process uses the file system's system call write (fd [1], buf, size) to send characters of size buf into the pipe entry fd [1], and the receiving process uses the system call read (fd [0], buf , size) read information from the pipe exit to buf. The pipeline transmits messages on a first-in-first-out basis. Only one-way transmission.
The system call to create an unnamed pipe is int fd [2], pipe (fd). ------- Reference textbook Computer Operating System Tutorial (Third Edition) Tsinghua University Press

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?