interprocess communication overviewclasses: 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:
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:
to create a working client, the programmer must:
data transfer
data transferthese are the ways that data can be transferred from one application to another. these are methods of wxconnection.
the default data type is wxcf_text (ascii text), and the default data size is the length of the null-terminated string. windows-specific data types could also be used on the pc.
examplessee the sample programs server and client in the ipc samples directory. run the server, then the client. this demonstrates using the execute, request, and poke commands from the client, together with an advise loop: selecting an item in the server list box causes that item to be highlighted in the client list box.
more dde detailsa wxclient object initiates the client part of a client-server dde-like (dynamic data exchange) conversation (available in both windows and unix). to create a client which can communicate with a suitable server, you need to derive a class from wxconnection and another from wxclient. the custom wxconnection class will receive communications in a 'conversation' with a server. and the custom wxserver is required so that a user-overridden wxclient::onmakeconnection member can return a wxconnection of the required class, when a connection is made. for example:
class myconnection: public wxconnection { public: myconnection(void)::wxconnection() {} ~myconnection(void) { } bool onadvise(const wxstring& topic, const wxstring& item, char *data, int size, wxipcformat format) { wxmessagebox(topic, data); } }; class myclient: public wxclient { public: myclient(void) {} wxconnectionbase *onmakeconnection(void) { return new myconnection; } };here, myconnection will respond to onadvise messages sent by the server by displaying a message box. when the client application starts, it must create an instance of the derived wxclient. in the following, command line arguments are used to pass the host name (the name of the machine the server is running on) and the server name (identifying the server process). calling wxclient::makeconnection implicitly creates an instance of myconnection if the request for a connection is accepted, and the client then requests an advise loop from the server (an advise loop is where the server calls the client when data has changed).
wxstring server = "4242"; wxstring hostname; wxgethostname(hostname); // create a new client myclient *client = new myclient; connection = (myconnection *)client->makeconnection(hostname, server, "ipc test"); if (!connection) { wxmessagebox("failed to make connection to server", "client demo error"); return null; } connection->startadvise("item"); |