Interprocess communication overview

Classes: wxServer, wxConnection, wxClient

wxWidgets has a number of different classes to help with interprocess communication and network programming. This section only discusses one family of classes - the DDE-like protocol - but here's a list of other useful classes:

wxWidgets' DDE-like protocol is a high-level protocol based on Windows DDE. There are two implementations of this DDE-like protocol: one using real DDE running on Windows only, and another using TCP/IP (sockets) that runs on most platforms. Since the API and virtually all of the behaviour is the same apart from the names of the classes, you should find it easy to switch between the two implementations.

Notice that by including <wx/ipc.h> you may define convenient synonyms for the IPC classes: wxServer for either wxDDEServer or wxTCPServer depending on whether DDE-based or socket-based implementation is used and the same thing for wxClient and wxConnection.

By default, the DDE implementation is used under Windows. DDE works within one computer only. If you want to use IPC between different workstations you should define wxUSE_DDE_FOR_IPC as $0$ before including this header - this will force using TCP/IP implementation even under Windows.

The following description refers to wx... but remember that the equivalent wxTCP... and wxDDE... classes can be used in much the same way.

Three classes are central to the DDE-like API:

  1. wxClient. This represents the client application, and is used only within a client program.
  2. wxServer. This represents the server application, and is used only within a server program.
  3. wxConnection. This represents the connection from the client to the server - both the client and the server use an instance of this class, one per connection. Most DDE transactions operate on this object.

Messages between applications are usually identified by three variables: connection object, topic name and item name. A data string is a fourth element of some messages. To create a connection (a conversation in Windows parlance), the client application uses wxClient::MakeConnection to send a message to the server object, with a string service name to identify the server and a topic name to identify the topic for the duration of the connection. Under Unix, the service name may be either an integer port identifier in which case an Internet domain socket will be used for the communications or a valid file name (which shouldn't exist and will be deleted afterwards) in which case a Unix domain socket is created.

SECURITY NOTE: Using Internet domain sockets is extremely insecure for IPC as there is absolutely no access control for them, use Unix domain sockets whenever possible!

The server then responds and either vetoes the connection or allows it. If allowed, both the server and client objects create wxConnection objects which persist until the connection is closed. The connection object is then used for sending and receiving subsequent messages between client and server - overriding virtual functions in your class derived from wxConnection allows you to handle the DDE messages.

To create a working server, the programmer must:

  1. Derive a class from wxConnection, providing handlers for various messages sent to the server side of a wxConnection (e.g. OnExecute, OnRequest, OnPoke). Only the handlers actually required by the application need to be overridden.
  2. Derive a class from wxServer, overriding OnAcceptConnection to accept or reject a connection on the basis of the topic argument. This member must create and return an instance of the derived connection class if the connection is accepted.
  3. Create an instance of your server object and call Create to activate it, giving it a service name.

To create a working client, the programmer must:

  1. Derive a class from wxConnection, providing handlers for various messages sent to the client side of a wxConnection (e.g. OnAdvise). Only the handlers actually required by the application need to be overridden.
  2. Derive a class from wxClient, overriding OnMakeConnection to create and return an instance of the derived connection class.
  3. Create an instance of your client object.
  4. When appropriate, create a new connection using wxClient::MakeConnection, with arguments host name (processed in Unix only, use `localhost' for local computer), service name, and topic name for this connection. The client object will call OnMakeConnection to create a connection object of the derived class if the connection is successful.
  5. Use the wxConnection member functions to send messages to the server.



ymasuda 平成17年11月19日