File: wx301.htm

package info (click to toggle)
wxwin2-doc 2.01-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,540 kB
  • ctags: 5,968
  • sloc: cpp: 15,157; makefile: 434; sh: 6
file content (194 lines) | stat: -rw-r--r-- 8,917 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<HTML>
<head><title>Interprocess communication overview</title></head>

<BODY BGCOLOR=#FFFFFF>
<A NAME="ipcoverview"></A><CENTER>
<A HREF="wx.htm"><img align=center src="contents.gif" BORDER=0 ALT="Contents"></A> <A HREF="wx278.htm#overviews"><img align=center src="up.gif" BORDER=0 ALT="Up"></A> <A HREF="wx300.htm#roughguide"><img align=center src="back.gif" BORDER=0 ALT="Previous"></A> <A HREF="wx302.htm#printingoverview"><img align=center src="forward.gif" BORDER=0 ALT="Next"></A> </CENTER><HR>

<H2>Interprocess communication overview</H2>
<P>
Classes: <A HREF="wx68.htm#wxddeserver">wxDDEServer</A>, <A HREF="wx67.htm#wxddeconnection">wxDDEConnection</A>, 
<A HREF="wx66.htm#wxddeclient">wxDDEClient</A>, 
<A HREF="wx238.htm#wxtcpserver">wxTCPServer</A>, <A HREF="wx237.htm#wxtcpconnection">wxTCPConnection</A>, 
<A HREF="wx236.htm#wxtcpclient">wxTCPClient</A><P>
wxWindows 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:<P>
<UL>

<LI> <A HREF="wx211.htm#wxsocketevent">wxSocketEvent</A>, 
<A HREF="wx209.htm#wxsocketbase">wxSocketBase</A>, 
<A HREF="wx210.htm#wxsocketclient">wxSocketClient</A>, 
<A HREF="wx213.htm#wxsocketserver">wxSocketServer</A>: classes for the low-level TCP/IP API.
<LI> <A HREF="wx186.htm#wxprotocol">wxProtocol</A>, <A HREF="wx254.htm#wxurl">wxURL</A>, <A HREF="wx105.htm#wxftp">wxFTP</A>, wxHTTP: classes
for programming popular Internet protocols.
</UL>
<P>
Further information on these classes will be available in due course.<P>
wxWindows has 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 is the same apart from the names of the classes, you
should find it easy to switch between the two implementations.<P>
The following description refers to 'DDE' but remember that the equivalent wxTCP... classes
can be used in much the same way.<P>
Three classes are central to the DDE API:<P>
<OL>

<LI> wxDDEClient. This represents the client application, and is used
only within a client program.
<LI> wxDDEServer. This represents the server application, and is used
only within a server program.
<LI> wxDDEConnection. This represents the connection from the current
client or server to the other application (server or client), and can be used
in both server and client programs. Most DDE
transactions operate on this object.
</OL>
<P>
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 sends the message
MakeConnection to the client 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 must contain an
integer port identifier.<P>
The server then responds and either vetos the connection or allows it.
If allowed, a connection object is created which persists until the
connection is closed.  The connection object is then used for subsequent
messages between client and server.<P>
To create a working server, the programmer must:<P>
<OL>

<LI> Derive a class from wxDDEServer.
<LI> Override the handler OnAcceptConnection for accepting or rejecting a connection,
on the basis of the topic argument. This member must create and return a connection
object if the connection is accepted.
<LI> Create an instance of your server object, and call Create to
activate it, giving it a service name.
<LI> Derive a class from wxDDEConnection.
<LI> Provide handlers for various messages that are sent to the server
side of a wxDDEConnection.
</OL>
<P>
To create a working client, the programmer must:<P>
<OL>

<LI> Derive a class from wxDDEClient.
<LI> Override the handler OnMakeConnection to create and return
an appropriate connection object.
<LI> Create an instance of your client object.
<LI> Derive a class from wxDDEConnection.
<LI> Provide handlers for various messages that are sent to the client
side of a wxDDEConnection.
<LI> When appropriate, create a new connection by sending a MakeConnection
message to the client object, with arguments host name (processed in Unix only),
service name, and topic name for this connection. The client object will call OnMakeConnection
to create a connection object of the desired type.
<LI> Use the wxDDEConnection member functions to send messages to the server.
</OL>
<P>
<A HREF="#topic1134">Data transfer</A><BR>
<A HREF="#topic1135">Examples</A><BR>
<A HREF="#topic1136">More DDE details</A><BR>
<P>

<HR>
<A NAME="topic1134"></A>
<H3>Data transfer</H3>
<P>
These are the ways that data can be transferred from one application to
another.<P>
<UL>

<LI> <B>Execute:</B> the client calls the server with a data string representing
a command to be executed. This succeeds or fails, depending on the
server's willingness to answer. If the client wants to find the result
of the Execute command other than success or failure, it has to explicitly
call Request.
<LI> <B>Request:</B> the client asks the server for a particular data string
associated with a given item string. If the server is unwilling to
reply, the return value is NULL. Otherwise, the return value is a string
(actually a pointer to the connection buffer, so it should not be
deallocated by the application).
<LI> <B>Poke:</B> The client sends a data string associated with an item
string directly to the server. This succeeds or fails.
<LI> <B>Advise:</B> The client asks to be advised of any change in data
associated with a particular item. If the server agrees, the server will
send an OnAdvise message to the client along with the item and data.
</UL>
<P>
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.<P>

<HR>
<A NAME="topic1135"></A>
<H3>Examples</H3>
<P>
See the sample programs <I>server</I> and <I>client</I> 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.<P>

<HR>
<A NAME="topic1136"></A>
<H3>More DDE details</H3>
<P>
A wxDDEClient object represents the client part of a client-server DDE
(Dynamic Data Exchange) conversation (available in both
Windows and Unix).<P>
To create a client which can communicate with a suitable server,
you need to derive a class from wxDDEConnection and another from wxDDEClient.
The custom wxDDEConnection class will intercept communications in
a 'conversation' with a server, and the custom wxDDEServer is required
so that a user-overriden <A HREF="wx66.htm#wxddeclientonmakeconnection">wxDDEClient::OnMakeConnection</A> member can return
a wxDDEConnection of the required class, when a connection is made.<P>
For example:<P>
<PRE>
class MyConnection: public wxDDEConnection
{
 public:
  MyConnection(void)::wxDDEConnection(ipc_buffer, 3999) {}
  ~MyConnection(void) { }
  bool OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format)
  { wxMessageBox(topic, data); }
};

class MyClient: public wxDDEClient
{
 public:
  MyClient(void) {}
  wxConnectionBase *OnMakeConnection(void) { return new MyConnection; }
};

</PRE>
Here, <B>MyConnection</B> will respond to <A HREF="wx67.htm#wxddeconnectiononadvise">OnAdvise</A> messages sent
by the server.<P>
When the client application starts, it must create an instance of the derived wxDDEClient. 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 <A HREF="wx66.htm#wxddeclientmakeconnection">wxDDEClient::MakeConnection</A>
implicitly creates an instance of <B>MyConnection</B> if the request for a
connection is accepted, and the client then requests an <I>Advise</I> loop
from the server, where the server calls the client when data has changed.<P>
<PRE>
  wxString server = "4242";
  wxString hostName;
  wxGetHostName(hostName);

  // Create a new client
  MyClient *client = new MyClient;
  connection = (MyConnection *)client-&gt;MakeConnection(hostName, server, "IPC TEST");

  if (!connection)
  {
    wxMessageBox("Failed to make connection to server", "Client Demo Error");
    return NULL;
  }
  connection-&gt;StartAdvise("Item");
</PRE>
Note that it is no longer necessary to call wxDDEInitialize or wxDDECleanUp, since
wxWindows will do this itself if necessary.<P>

</BODY></HTML>