Navigation:  Programming Cookbook > Sockets Connectivity > Talking Objects across a Socket Connection >

Convenience methods for reading and writing objects

Previous pageReturn to chapter overviewNext page

To avoid the need to create explicit STB Filer objects, we have added a couple of convenience methods to the Socket class for sending and receiving objects. You can use the #send: method to send an object using a dynamically created STBOutFiler and then, at the destination Socket, use #receive to receive it. The only disadvantage when using these methods is that they are slightly less efficient for sending large numbers of objects since a new STB Filer must be created for each one.


Let's illustrate the bare bones of a "Smalltalk Server". We'll get the client to send bits of Smalltalk source to the server for evaluation, and the results will then be sent back to the client for display.

In the Server workspace, start-up a server process:

serverProcess := [[answer := Compiler evaluate: socketA receive logged: false.

  socketA send: answer] repeat] fork.


This will repeatedly take String requests from the server Socket and use the compiler to evaluate them. The resulting answer object is then sent back to the client for display.

In the Client workspace, send some requests to the server and receive the results:

socketB send: '3+4'; receive. "Display it"

socketB send: '200 factorial'; receive. "Display it"


Obviously, you want to be somewhat careful about the requests you make of the server in this demonstration. Remember, it is serializing the actual result object across the Socket link (not just its textual representation) so, if this is very large, you might end up waiting a long time for the operation to complete. Also, be aware that not all objects are serializable using the STB mechanism.

When you've finished, shut down the server process to tidy up:

serverProcess terminate.