|
|||||||||||||||||||
Create a new visual part 'MyServerWindow' in 'MyServerApp' and open it with
the Composition Editor. Connect the 'openedWidget' event of the window to a new
method #startServer and connect the 'aboutToCloseWidget' event of the window to
a new method #stopServer. Drop a new Text part into the window and call it
'Text', say. Connect its 'string' event to a new method #textStringChanged.
Before you create the body of those methods add the instance variable names
'server' and 'semaphores' to 'MyServerWindow'. Then edit the methods so they
look like these:
startServer
semaphores := Set new. "Create a new Stream Server listening on port 9000" server := TobSocketsStreamServer
clientBlock: [:cs | | semaphore | "Client has connected" semaphore := Semaphore new. semaphores add: semaphore. "Keep looping until and exception occurs. An exception will occur when the client disconnects." [
cs
cr. semaphore wait. true
"The client is no longer interested in changes to 'Text'." semaphores remove: semaphore simultaneously in the same image like this." server forkAt: (Processor userBackgroundPriority) |
stopServer
|
textStringChanged
"Signal each semaphore to give the client the new value" semaphores do: [:sem |
|
Save the part. Now test the part to start the server. To create a client open
an application such as 'telnet' in Windows (from the 'Start' menu select
'Run...', type 'telnet' and press 'OK'). Use it to Connect to your local machine
on port 9000. As you type in the server window you should see the text appear in
the telnet application window.
If you do need to modify the #startServer method (say, to use a different
port number), make sure that you first close the window to stop the server.
Once the server seems to be behaving correctly, close the window and build a standalone application with 'MyServerWindow' set to open at startup. Run the application to check that it runs as it did in the development image.
Create a new visual part 'MyClientWindow' in 'MyClientApp' and open it with
the Composition Editor. Connect the 'openedWidget' event of the window to a new
method #connectToServer and connect the 'aboutToCloseWidget' event of the window
to a new method #disconnectFromServer. Drop a new Label part into the window and
call it 'Label', say. Before you create the body of the methods add the instance
variable names 'socket' and 'process' to 'MyClientWindow'. Then edit the methods
so they look like these:
connectToServer
socket := TobSocketsSocket new. socket connect: (
portNumber: 9000). Instead it should wait until there is something to get." socket upToTimeOut: 0. "Wait for new data in a background process." process := [
[
(self subpartNamed: 'Label') labelString: (socket nextLine). true
|
aboutToCloseWindow
process isNil ifFalse: [process terminate] |
Save the part and test it while the server application is running. The text
that you type in the server window should appear in the label in the client.
This web page shows just the basics. It would be easy to extent these ideas
further so that Smalltalk objects are exchanged (rather than strings) by
incorporating the ObjectSwapper. Or maybe the server could be used to serve many
different pieces of information named by the client. You would also probably
want to improve the exception handling at client connection to make it more
robust.
Here at Totally Objects we are keen to find out the creative ways that you
are using our products in your applications. Please email us at ideas@totallyobjects.com to share
your ideas.
|
|
|
| Back to SocketSet Home Page | Back to Totally Objects Home Page |