Herbert said
<<Some code:
Socket initializeNetwork "Only once per image" dst := Socket newUDP setPort: 1979. src := Socket newUDP setPort: 2345. buff := ByteArray new: 1026. delay := Delay forMilliseconds: 10. localhost := ByteArray withAll: { 192. 168. 42. 11 }. received := nil.
rp := [[ received := dst receiveUDPDataInto: buff. received first isZero ] whileTrue: [ delay wait ]. WorldState addDeferredUIMessage: [ { buff. received } inspect ]] fork.
src sendUDPData: ( 'ctmame' , (String value: 10) , (String value: 162)) toHost: localhost port: 1979.
rp terminate. src closeAndDestroy. dst closeAndDestroy.
src := nil. dst := nil. buff := nil. delay := nil. rp := nil.
Smalltalk garbageCollect Socket allInstances>>
That's great, thank you very much. I have a couple of other questions that sprang from using this code. 1. I couldn't get the WorldState addDeferredUIMessage: to do anything. What is supposed to happen? I am running the latest Pharo build. The class exists, and I didn't get a DNU, but I didn't get anything else either!
2. Out of curiosity, I tried used the NetNameResolver addressForName: method as a way of getting a socket address (instead of using the ByteArray. I tried this when nothing was working, and I noticed the method call in several of the Socket methods. However, Squeak gave me a DNU. Is there a simple way to enter host names using the NetNameResolver, or have I completely misunderstood its function?
Cheers Andy
Hi Andy,
AB> That's great, thank you very much. I have a couple of other AB> questions that sprang from using this code. AB> 1. I couldn't get the WorldState addDeferredUIMessage: to do AB> anything. What is supposed to happen? I am running the latest AB> Pharo build. The class exists, and I didn't get a DNU, but I AB> didn't get anything else either!
uh yeah I wasn't sure what approach you would choose and I was about to leave for work. Sorry.
First I'm on Squeak 3.8 so let me try Pharo.
All works fine in what I just downloaded from Pharo Website.
So, after you added the deferred UI message, the next line (sendUDPData...) should pop up an Inspector with an Array. I found the deferred UI message trick somewhere in the archives or maybe got direct help from someone. You won't need it.
You need two images. In the receiver you build a loop in which you do something like:
commSocket dataAvailable ifTrue: [receiveResult := commSocket receiveUDPDataInto: receiveBuffer. "now display your time in a big TextMorph"] ifFalse: [myDelay wait]
Doing real time asynchronous things with the Squeak UI is a bit tricky but following the lead of ClockMorph should get you going.
The common animation methods in Squeak isolate you from that problems but changing a Morph's shape at the wrong moment is not good. Sorry I can't be more specific but if you report problems I will remember them, I did several animations of lots of Morphs.
AB> 2. Out of curiosity, I tried used the NetNameResolver AB> addressForName: method as a way of getting a socket address AB> (instead of using the ByteArray. I tried this when nothing was AB> working, and I noticed the method call in several of the Socket AB> methods. However, Squeak gave me a DNU. Is there a simple way to AB> enter host names using the NetNameResolver, or have I completely AB> misunderstood its function?
no, you haven't. I used the code in a network with fixed IP's and no DHCP running. Right here using NetNameResolver works just fine. If you doIt:
(NetNameResolver addressForName: 'aComputersName') inspect
you should get an Inspector on a ByteArray with the IP address.
Cheers,
Herbert
beginners@lists.squeakfoundation.org