How to do networking in Squeak?

Michael Roberts mike at mjr104.co.uk
Tue Feb 3 12:15:03 UTC 2004


Hi,

Göran and Lex make good points, but I thought I would show a slightly different way of doing the same thing.

In a workspace type 

Socket newTCP

and inspect it twice - this gives you two inspectors.  move them so that they are side by side.  In the left one type

self listenOn: 8080

in the bottom evaluation pane and do-it.  Click on 'self' in the inspector and check that the socket is waiting.

Go the the right hand inspector and type

self connectTo: NetNameResolver localHostAddress port: 8080

and do-it.  If you have both inspectors showing the state of 'self' then you should immediately see that they have both 'connected'.

In the right hand one then type

self sendData: 'hello'

and do-it.  And in the left one

self receiveData

and print-it.  It should say 'hello'.  You can then do anything you like in these two inspectors.  You have successfully created a TCP connection and sent some data.

To then find out what other messages you can send you can either browse the class directly by alt-b on 'self' or look at its protocol through alt-p.

rather than using Socket>>listenOn: you could use Socket>>listenOn:backlogSize: and then inspect 

self accept 

to get an inspector for each incoming connection.

Some messages that you can send to sockets can block for a reasonable timeout depending on various things so you can always alt-. to get the ui back if it seems to freeze.

Before you close the inspectors it is worth doing a

self closeAndDestroy

in each one so that you release the socket's resources.

Cheers

Mike




More information about the Squeak-dev mailing list