) Various channels for inter-program communication and Analysis

Source: Internet
Author: User
Tags ibm db2 mqseries readfile
I. Opening
Program The main purpose of inter-communication is to achieve data sharing and information exchange between applications on multiple computers (or the same server. In different computer systems, data sharing and information exchange can only be achieved through protocols between networks. In the same computer system, data sharing and information exchange can be achieved through only a certain channel between them. There are many similarities and characteristics in program communication between different computer systems and the same computer system. Inter-program communication relies on certain channels (PIPE). The channels are diverse and distinctive.
In order to fully understand and master inter-program communication and its corresponding implementation technologies, this article discusses various communication methods. Including the principles and implementations of each method. Ii. various communication channels and implementation
First, programs are independent of each other, and their runtime environment is not changed by other processes. I. [socket]
Socket programming is a typical method of session programming, similar to home visits by teachers, knocking on the door ---- someone opens the door ---- go in ---- communicate ---- go out. It is applicable to Client/Server Communication and point-to-point communication.
The following describes the specific tasks of the server and client respectively. This section describes the TCP process.
(1) The server first creates a socket and calls it using socket (); then uses BIND () associate the socket with a local IP address and a port (the port can be idle or non-idle. For details, refer to the implementation of port listening .) Article ). Use listen () to wait for the socket to enter the connection, and then use accept () to prepare the socket for accepting the client connection. When the connection request arrives, the accept () function of the blocked service process generates a new socket to establish a connection with the customer socket and return a receiving signal to the customer. Read () is used to read data, and write () is used to write back some data to the sending process, such as confirmation information or Echo information. (2) The Client client process also creates a socket and uses Socket () to call it. Then, the client sends a connection request to the service process. By calling connect (), the client can establish a connection, after the connection is successful, write () is used to send data to the server, and read () is used to read the data returned by the server. This method implies the asymmetry of communication between clients and servers. The client/server model requires a set of conventions agreed upon by the client and server to ensure that the server can be provided (or accepted). This Convention includes a set of protocols, it must be implemented at both ends of the communication. Depending on the actual situation, the protocol may be symmetric or asymmetric. In symmetric protocols, each party may assume a master-slave role. In asymmetric protocols, each side is a slave. For example, terminal simulation Telnet is a symmetric protocol, and HTTP is a non-symmetric protocol. Whether the specific Protocol is symmetric or asymmetric, the customer process and service process must exist when the service is provided. This communication method is applicable to a single computer system and multiple computer systems.

Wsadata;
Word version = makeword (2, 0 );
Int ret = wsastartup (version, & wsadata );
If (Ret! = 0)
Trace ("initilize error! \ N ");
M_hsocket = socket (af_inet, sock_stream, 0 );
M_addr.sin_family = af_inet;
M_addr.sin_addr.s_un.s_addr = inaddr_any;
M_addr.sin_port = htons (m_nport); int ret = 0;
Int error = 0;
Ret = BIND (m_hsocket, (lpsockaddr) & m_addr, sizeof (m_addr ));
If (ret = socket_error ){
Trace ("BIND error: % d \ n", (error = wsagetlasterror ()));
Return;
}
Ret = listen (m_hsocket, 2 );
If (ret = socket_error ){
Trace ("Listen error: % d \ n", (error = wsagetlasterror ()));
Return;
}
Socket S = accept (m_hsocket, null, null );
If (S = socket_error ){
Trace ("Accept error: % d \ n", (error = wsagetlasterror ()));
Return;
} Char buff [256];
Ret = Recv (S, buff, 256, 0 );
If (ret = 0 | ret = socket_error ){
Trace ("Recv data error: % d \ n", wsagetlasterror ());
Return;
}
Char Buf [] = "hello ";
Send (S, Buf, str. getlength (), 0); II.
This method is very primitive, with low usage and disk space occupation. Using a file as a channel for inter-program communication, program a writes data to a file, and program B reads the data in the file to communicate with program, at the same time, this process can be reversed. The premise for this method is that the user running the program must have the read/write permission on the disk.
| A. writefile ------> filea <------ B. readfile (check every <time>)
| A. readfile (check every <time>) -----> fileb <----- B. writefile ●
//
Int buff = 0;
Ofstream table;
Table. Open ("C: \ temp.txt ");
Table <buff <Endl;
Table. Close (); // B
Int buff = 1;
For (;;)
{
Ifstream table;
Table. Open ("C: \ temp.txt ");
Table> Buff;
Table. Close ();
If (buff = 0 ){
Printf ("operation code equal 0 ");
//...
Return;
}
Sleep (10 );
} This method is applicable to a single computer system and multiple computer systems. This method is used to control and exchange data between multiple computer systems. You can place this file on a computer, and the other party establishes an empty connection. copy the file to a local computer for reading. There are also many other methods. Iii. [Signal]
This method is only used for process control and cannot be used for data exchange. For example, when illegal memory access occurs, invalid commands are executed, and some buttons (such as Ctrl + C and del) generate a signal, system calls related to other program calls or custom processing processes.
The system call of information processing is signal, and its prototype is void (* signal (INT Sig, void (_ cdecl * func) (INT sig [, int subcode]). (INT sig); this function allows a process to select one of the many methods to process the interrupt signal returned by the operating system. Based on similar ideas, we can also use "signals" between multiple programs for inter-program communication.
The key to this method is to generate signals. Keybd_evnet () can be used to generate signals. This function can simulate keyboard output. Its prototype is void keybd_event (byte bvk, byte bscan, DWORD dwflags, DWORD dwextrainfo ); vvk is a virtual key code, and bscan is a scan code, corresponding to the corresponding key. This method is applicable to a single computer system. Iv. [pipe]
Pipe is a simple mechanism for inter-process communication (IPC). It can be divided into two types: Unknown pipelines and named pipelines. The named pipe can communicate two-way between different processes on the same machine and between different processes on different machines (using UNC naming rules ). The biggest benefit of an MPS queue is that it can operate like a normal file. Its operation identifier is handle, that is, it can use readfile, the writefile function performs read/write operations unrelated to the underlying implementation. You do not have to understand the details of inter-network/inter-process communication.
An unknown MPs queue is actually a temporary storage zone in the memory. It is controlled by the system security and is independent of the memory zone in which the process is created. Pipelines manage data in a FIFO manner and operate in strict order. Pipelines cannot be searched and information in pipelines can only be read once. The unknown pipeline only communicates between parent and child processes or between two sub-processes of a process. It is unidirectional. An unnamed pipeline is actually implemented by a famous Pipeline with a specified name.
The operations of famous pipelines are similar to those of unknown pipelines.
The createpipe () function is used to create an MPS queue, and readfile () and writefile () are used to operate it. ●
Security_attributes;
Handle hreadpipe, hwritepipe;
Createpipe (& hreadpipe, & hwritepipe, & A, 0 );
Unsigned long lbytesread;
Char buff [1024];
Int ret;
While (1)
{
Ret = peeknamedpipe (hreadpipe, buff, 1024, & lbytesread );
If (lbytesread)
{
Ret = readfile (hreadpipe, buff, lbytesread, & lbytesread, 0 );
If (! RET) break;
}
Else
{
// Lbytesread = ...;
If (lbytesread <= 0) break;
Ret = writefile (hwritepipe, buff, lbytesread, & lbytesread, 0 );
If (! RET) break;
}
} V. [MQ]
The message communication mode uses the message buffer as the intermediate medium. Both parties send and receive messages in units. In memory, the message buffer is organized into a queue, which is usually called MQ ). A message queue is a storage area independent of the processes that generate it. Any process with the correct access permission can access the message queue. It is very suitable for exchanging short messages between processes.
Each message is classified by type number. A message queue will always exist after it is created until it is deleted using relevant calls. Vi. [shared storage segment (SM)]
Shared memory allows multiple processes to communicate with each other using the same memory segment as an intermediate medium in an external communication protocol or synchronization/mutex mechanism, it is a very effective data communication method.
Before communication, create a shared memory segment, and then perform the ing and separation operations. At the same time, you can change the access permissions of shared memory segments and other features as needed. ☆For the following centralized communication method, we should mention "Middleware (middleware )".
Middleware is a type of software that implies the details of the actual network and communication protocols for the application. Advanced programming interfaces help developers create applications in different environments without having to learn more about the networks and communication protocols they will use.

 

Normally, middleware is in a client/server environment that uses different network communication protocols. It can hide the protocol from the client/server application, so that developers can focus on improving the application, rather than developing communication interfaces. Middleware Products hide the differences between front-end applications and backend applications. The middleware layer provides translation between common applications and popular application programming interfaces (APIS. For example, Microsoft's Open Database Connection (ODBC) Standard provides general functions for backend database system operations. The front-end application writes data to ODBC and uses its functions. ODBC hides the differences between SQL implementations of different vendors. Microsoft provides ODBC in the form of a group of Microsoft Windows drivers to provide for Microsoft Access, Microsoft Excel, Microsoft SQL Server, Foxpro, BTRIEVE, dBase, access to data generated by Borland paradox, IBM DB2, decrdb, and Oracle. ODBC is designed to make windows a standard for customers to access backend databases. □Users need to access services on many different backend servers. □Backend servers can use different operating systems and require different communication protocols. □The backend database server has an incompatible SQL command set, which makes it difficult for users and programmers to switch from one system to another. □It is unrealistic to restrict users to use only certain applications that access backend services. Users need to access services from different applications. □The new model aims to enable users to access any backend service on any frontend using multiple protocols in a multi-vendor environment. In a multi-protocol and multi-vendor environment, a programmer usually needs to write an application to work with each protocol and support system. With middleware, programmers only need to simply write the middleware interface, and the middleware handles all the multi-protocol and multi-vendor issues. There are three types of middleware: Remote Procedure Call (RPC), conversations, and message passing systems. They can well hide the communication process and the differences between systems that operate on them. VII. [RPC]
Remote Procedure Call (PRC)
A Remote Procedure Call is a request from a computer system on the network to another computer system. RPC maintains the nature of middleware in different network platforms and communication protocols. Basically, a PRC is a direct request from a computer to another computer. It is a request/answer process. In this case, the request is waiting for a response, which means that PRC is usually a real-time call on the connection-oriented interface.
The two applications that the RPC mechanism enforces communication must be in the running state at the same time. For remote calls, the two must first establish a connection, and the quality of the communication link has a great impact on the effect.
It works in the following way: When an application a needs to exchange information with another Remote Application B or asks B to provide assistance, a will generate a local request through the communication link, comrade B receives information or provides corresponding services. After B completes relevant processing, it returns the confirmation information or results to.
The advantage of PRC is that the application uses the call/Return Method to communicate with each other, which has a high potential efficiency. However, the close coupling between applications is required, and the communication line must maintain a good state during the communication, in addition, a large amount of underlying communication programming is required. VIII. [conversations]
Conversations is a continuous session between two or more systems that are logically connected. Unlike RPC, in a distributed environment, meetings may overlap. For this reason, it is necessary and beneficial to modify the distributed database because it must be completely synchronized in multiple places. IBM's advanced program met with APPC. The OSI Standard for meeting implementation has also emerged. The communication Integrator (CI, communication Integrator) of covia technologies (Rosemont, Illinois) is another example of middleware with meeting that can run on large computers, computers, and desktops. IX. [MQSeries Message Queue]
To simplify the communication between applications, make the communication highly reliable, and ensure the simplicity of implementation, middleware technology is our first choice. IBM MQSeries is a product based on this technology. MQSeries is an independent communication software that automatically transfers information by submitting tasks to the software.
For example, application
|
|
Put | message
|
|
Bytes
MQSeries interface ----------------
Optional |
|
| Wake up
Read | message (in Q1) |
|
|
Application B (both local and remote) <----- message transmission between applications is implemented through queues and is indirect, so even if B fails to run normally, A can still run normally, and messages can also wake up B.
It can be seen that MQSeries ensures that the information is permanent and recoverable, ensures the security of information transmission, and ensures that the information is successfully sent only once. At the same time, in addition, each application can run independently.
Both common and private queues are opened using mqopenqueue (). The mqopenqueue function is used to open a queue for sending or reading messages. The prototype is hresult apientry mqopenqueue (lpcwstr lpwcsformatname, DWORD dwaccess, DWORD dw1_mode, lpqueuehandle phqueue). lpwcsformatname is the format name string specified by the user pointing to the queue, the format name can be public, private, or direct format. Dwaccess is a queue method used by user-specified applications. It can be peek, send, or receive. This setting cannot be changed when the queue is enabled. Dwaccess has three modes: mq_peek_access/mq_send_access/mq_receive_access. Dw1_mode is the sharing mode of the queue specified by the user. It can be divided into mq_deny_none and mq_deny_receive_share. mq_deny_none is the default value and can be used by anyone. If dwaccess is set to mq_send_access, it must be; mq_deny_receive_share limits the process of reading messages from this queue. If the queue has been opened by another process to receive messages, mq_error_sharing_violation and mq_deny_receive_share will be returned only when dwaccess is set to mq_receive_access or mq_peek_access. Phqueue is the handle of the output opened queue.
Then, you can use mqsendmessage ()/mqreceivemessage () and other functions to perform specific operations. Iii. Conclusion
The technology is accumulated in 1.1 drops, and it is necessary and useful to systematically master any knowledge point.
Various communication implementation technologies have their own characteristics and scope of use. technologies such as pipelines, message queues, and shared memory are most suitable for inter-process communication within the same computer system to ensure high efficiency. Remote Process calls, socket session programming, and MQSeries are most suitable for communication between remote applications. They can simplify communication programming and ensure the reliability of communication. In particular, MQSeries is a well-developed middleware product that is used by many information systems.
The rational use of various communication technologies often plays a multiplier role.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.