links to this page:    
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
Squeak, networks and examples
Last updated at 1:41 pm UTC on 15 February 2017



2006 and earlier

Lex Spoon January 19, 2004: If your goal is to do WWW stuff, you might look at this page I put together: "How to Mine Web Pages" http://coweb.cc.gatech.edu/cs2340/1271 It walks you through interacting with WWW forms; in the end, you have an IRC bot that translates stuff using Babelfish.

Question Kwiyi Woo February 01, 2004: I am trying to figure out how to do networking stuff in Squeak. I wrote a very simple code to create a listener at the specified port. But I don't know how to write back value so that the browser can display it. Could anyone show me a very simple example so that I can learn from it.
	| socket listener |
	listener _ Socket newTCP.
	listener listenOn: 8080.
		[socket _ listener waitForAcceptFor: 20.
		(socket notNil and: [socket isConnected])
		ifTrue: [Transcript show: 'hello World'.
		socket _ nil]
		ifFalse: [Transcript show: 'not success']].

Answer Goran Krampe: Best would be if you looked at Comanche: http://map1.squeakfoundation.org/sm/packagebyname/komhttpserver ...at least if you are interested in doing web stuff.

In general - ConnectionQueue is the heart of a socket server in Squeak, look at it. And SocketStream is the easiest class to use for client side stuff. It is higher level than Socket.

There are tons of packages on SqueakMap that do different kinds of networking. Install a few and look at how they do it. Also - if you plan on doing advanced server side networking the package SharedStreams may be of interest (multiple processes working on streams).

Answer Lex Spoon: OldSocket seems to have a bunch of examples to look at. Definitely look at them; they may not work as written, but they should give you a good idea of what is going on.

Also, you should learn about workspaces if you haven't already. They will let you run one command at a time and save the results into variables. So you can do this:
	listener _ Socket newTCP.
	listener listenOn: 8080.
And now the "listener" variable holds a socket that is listening. Then you can do something like this:
 	connector _ Socket newTCP.
	connector connectTo: (NetNameResolver localHostAddress) port: 8080.
And now the sockets are connected and you can do sendData: and receiveData.

But as Goran mentioned, raw sockets are just the tip of the iceberg. If there is a particular networking task you'd like to accomplish, you could probably get the task done faster by working at a higher level. HTTP and/or XML streams, for example.

Answer Michael Roberts: 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 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 the three key combo:“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.

CommentHolger Schuh
NetNameResolver localHostAddress
returns your "external ip" (and not, so if you have a firewall running on the machine you are trying this .... open tha port. Strange thing is that you have to start a new Socket when the connection was insuccessfull the first time. Great tutorial anyway.

See also Squeak, networks and examples