Several methods of communicating between applications

zhaozj2021-02-17  50

Data exchange between the applications Mutual communication (mutual communication) between the application (mutual communication) has been a problem that plagues the majority of programmers, although a variety of solutions have emerged, but there is no one so far The solution is perfect. Therefore, only after learning and understanding their advantages and disadvantages, the best solution can be selected in a specific case to meet the final requirements. 1. Sharing on the hard disk to create a file, one application writes data in this file (can not close the file, but must refresh the buffer), another application opens this file in a shared method and reads the content, this It is the easiest way of data exchange. For network users, as long as the two terminals are installed on both terminals, they are mapped to the network drive as long as they set a directory sharing, and data exchange can also be simply implemented. However, its shortcomings are also obvious: only the polling can get the latest data (inefficient), the network mapped drive absolutely cannot change or cancel (reliability difference), so this is a "low" communication method. 2, DDE Every Windows programmer does not feel unfamiliar with DDE (dynamic data exchange), it is the earliest Windows-based data exchange method, there are three ways to choose from: cold connection, warm connection and thermal connection. Generally, the client will issue an application to the server side and must specify the name and title of the server side. After the connection is established, the data can flow two-way flow. Typical examples such as graphics software Snagit, which provides DDE interfaces that allow other applications to control it. DDE is fully compatible, from 16-bit platform to 32 bits, and source code is hardly modified. DDE also has a network function. People who have used Win311 for Workgroup remember that it comes with a very attractive applet "chat" that allows two computers to talk through the network, which is almost a pioneering at the time. However, few people know that "chat" is a special DDE, namely NetDDE. Its basic working principle is still DDE, but it enables a computer to send a message to another terminal in the same network, and unlike ordinary DDE only limit on the same machine. DDE is no longer advanced compared to other data exchange methods, and Microsoft no longer actively supports DDE, so its prospect is not optimistic. 3, WM_COPYDATA Windows message WM_CopyData is powerful, and it is very little known. Its exact definition is that an application is sent to another application message when data is passed. As we all know, Windows depends on the message mechanism to a large extent, then why don't we send the data in the message? In this way, we only need to call sendMessage (), with the handle of the other party as the first parameter to contain the address of the pointer structure to the actual data as the second parameter, you can use the entire data block as a message to another application. The programmer may have said that a custom message can also complete such a job. He is only right to half. Custom messages can indeed send them to other applications, but the memory area where its original data is located is only legitimate. If you access the memory area in other modules, it will cause "Access Violation". This problem does not exist in WM_CopyData, because Windows itself will handle this for you.

In-depth drilling, you will find that it is actually created a file mapping object, copy the original data of the sender first to the mapping file, and then open a "view" on this mapping file on the receiver. The deficiency of WM_CopyData is that there must be a form to receive messages and data (lack of flexibility), and the data is copied to a mapping file before use (waste resources). 4, NetBIOS began in the 1980s, NetBIOS is the standard interface for developing the Client / Server class program. The other solutions at that time were almost developed from UNIX systems. Of course, for today's users, NetBIOS is no longer choice, they can choose from various programs. On Windows 95 and NT platforms, the NetBIOS feature can be called via the API function NetBIOS (). NetBIOS is compatible with the following low-level protocols: NetBeui Protocol (NBF), NWLink NetBIOS (NWNBLINK), NetBIOS over TCP / IP (NetBT). The advantage of NetBIOS is faster, and the disadvantage is poor reliability. 5. Standard Sockets and Winsock Sockets are ranked 15 years ago, which is mainly used for local communication, but it is now applied to the Client / Server system. Its kernel is very simple: you can view a socket as a two-way node, an application can establish a connection with another program (on the end of both parties to behave in order to distinguish between simultaneous operation Communication thread), then exchange data with each other. Microsoft created Winsock on the basis, specifically used for Windows interfaces, which is fully compatible with Socket. In recent years, based on the TCP / IP protocol, Socket has also gained a wider application. Today, Sockets has gained the most widely used applications on the Internet, mainly because of its portability: Socket applications can communicate with each other in any platform (regardless of the PC or Macintosh, no matter what Windows platform Or UNIX platform). The latest Winsock 2.0, not only based on TCP / IP protocol, but also based on several other transmission protocols (such as IPX). Socket's defect is that it works on the low level of communication, so it is more complicated (if it is a Win 95 or NT platform, there is no such problem, Microsoft provides corresponding controls). However, if you want to communicate among multiple platforms, Socket is no two. 6, Mailslot and Pipe Mailslot and PIPE have a lot of similar points, namely high-level, memory-based communication systems. MailSlot is created by the Server side, as follows: mymailslot = createmailslot (pslotname, 0, mail); then, the client can open this mailslot like an open file, and then write data to a message by using the API function writefile (). In the queue.

Similarly phase, Pipe is created by the Server side, as follows: pipe = CreateNamedPipe (pPipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE, 20000, NULL); Next, Client terminal can be by a general File API reads and writes write data, the code is as follows: Pipe = createfile (ppipename, generic_read | generic_write, 0, null, open_existing, 0, null); Success = Writefile (PIPE, PMESSAGE, STRLEN (PMESSAGE) 1, & Byteswritten, NULL; PIPE is divided into two, namely the PIPE and anonymous PIPE. An anonymous PIPE is identified by a handle instead of name, and thus it limits it to communicate on the same machine without being applied to the network. Name PIPE provides an identity with name, so you can open it anywhere on the network. It should be noted that named PIPE can only be created on WIN NT instead of Win 95, and the client end can be any platform. PIPE can either one-way communication or two-way communication, MAILSLOT can only communicate one-way communication. The Client side can send a message to mailslot, but cannot receive a message; if you want to receive a message, you can only create a new Mailslot. But Mailslot has a big advantage: it supports data broadcasting. That is, if a message sends a message, the same name object in the entire network can be received. This is because the scope of Mailslot's name is just on this machine, so you can create the same name on different machines. When the Client is sent, Mailslot on each machine has received a copy of the message. And make a corresponding response on this unit. MaxLOT's maximum defect is unreliable because its data is transmitted in datagram format, and network errors or load will cause data loss. PIPE is more reliable, but it cannot broadcast. So, if you don't need to broadcast, PIPE is a better choice. If you choose Mailslot, you have to be fully prepared for the trouble you encounter. 7. Clipboard (CLIPBOARD) Do you feel unfamiliar with the clipboard of Windows? Its appearance is to implement mutual data exchange between applications. Windows provides a range of API functions to allow applications to securely open the clipboard, read and write data. The defect of the clipboard is also obvious: When there is a new data on the clipboard, the previous data will be rushed away, and it is too much when using the clipboard in Windows, so this way Communications for the program are obviously not safe enough. 8, COM and DCOM Public Object Mode (COM) are an agreement that establishes a software module connection between another software module and then describes it. When this connection is established, the two modules can be communicated by a mechanism called "interface".

转载请注明原文地址:https://www.9cbs.com/read-28844.html

New Post(0)