Hello 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 please show me a very simple example so that I can leary from it. Thanks Kwiyi ___________________________________________________________________ start | socket listener | listener _ Socket newTCP. listener listenOn: 8080. [true] whileTrue: [socket _ listener waitForAcceptFor: 20. (socket notNil and: [socket isConnected]) ifTrue: [Transcript show: 'hello World'. socket _ nil] ifFalse: [Transcript show: 'not success']]. ___________________________________________________________________
_________________________________________________________________ Check out the coupons and bargains on MSN Offers! http://shopping.msn.com/softcontent/softcontent.aspx?scmId=1418
Hi all!
"Kwiyi Woo" kwiyi@hotmail.com wrote:
Hello 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 please show me a very simple example so that I can leary from it. Thanks Kwiyi
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.
regards, Göran
PS. There are tons of packages on SqueakMap that does different kinds on 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).
Hmm, 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.
-Lex
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
squeak-dev@lists.squeakfoundation.org