[Pharo-project] UDP Listener example?
I'm trying to build a display for some instruments that multicast readings using UDP. I started out using CocoaAsyncSocket on Mac (https://github.com/robbiehanson/CocoaAsyncSocket) and I have to say it has a really wonderful api - would be worth cloning it. However, want to do the same thing in Pharo so I have typed this little snippet into the workspace: [| socket | socket := Socket newUDP. 10 timesRepeat: [| s | s := String new. socket receiveDataInto: s fromHost: (NetNameResolver addressFromString:'255.255.255.255' ) port: 4848. Transcript show: s.]] fork and it doesn't seem to do anything. What have I missed? Pharo 1.3 stable on SnowLeopard BTW. Thanks, -Todd Blanchard
Hi Todd, The socket chapter in writing progress could help you: https://gforge.inria.fr/scm/viewvc.php/*checkout*/PharoByExampleTwo-Eng/Sock... I do not remember if the receiveData method is blocking. If not, you should wait til data arrives. Cheers, Jannik On Feb 6, 2012, at 22:22 , Eagle Offshore wrote:
I'm trying to build a display for some instruments that multicast readings using UDP. I started out using CocoaAsyncSocket on Mac (https://github.com/robbiehanson/CocoaAsyncSocket) and I have to say it has a really wonderful api - would be worth cloning it.
However, want to do the same thing in Pharo so I have typed this little snippet into the workspace:
[| socket | socket := Socket newUDP. 10 timesRepeat: [| s | s := String new. socket receiveDataInto: s fromHost: (NetNameResolver addressFromString:'255.255.255.255' ) port: 4848. Transcript show: s.]] fork
and it doesn't seem to do anything.
What have I missed? Pharo 1.3 stable on SnowLeopard BTW.
Thanks, -Todd Blanchard
--- Jannik Laval
Thanks, I found this but it pretty well ignores UDP other than mentioning it exists. On Feb 6, 2012, at 1:38 PM, jannik.laval wrote:
Hi Todd,
The socket chapter in writing progress could help you: https://gforge.inria.fr/scm/viewvc.php/*checkout*/PharoByExampleTwo-Eng/Sock... I do not remember if the receiveData method is blocking. If not, you should wait til data arrives.
Cheers, Jannik
On Feb 6, 2012, at 22:22 , Eagle Offshore wrote:
I'm trying to build a display for some instruments that multicast readings using UDP. I started out using CocoaAsyncSocket on Mac (https://github.com/robbiehanson/CocoaAsyncSocket) and I have to say it has a really wonderful api - would be worth cloning it.
However, want to do the same thing in Pharo so I have typed this little snippet into the workspace:
[| socket | socket := Socket newUDP. 10 timesRepeat: [| s | s := String new. socket receiveDataInto: s fromHost: (NetNameResolver addressFromString:'255.255.255.255' ) port: 4848. Transcript show: s.]] fork
and it doesn't seem to do anything.
What have I missed? Pharo 1.3 stable on SnowLeopard BTW.
Thanks, -Todd Blanchard
--- Jannik Laval
Am 06.02.2012 um 23:13 schrieb Eagle Offshore:
Thanks, I found this but it pretty well ignores UDP other than mentioning it exists.
I think you might confuse the host and port portions of it. If you use socket receiveDataInto:fromHost:port: then the host and the port are the ones of the sending side. You need to specify your own side as well | localPort sourceAddress sourcePort buffer receivedBytes socket delay | localPort := 5000. sourceAddress := NetNameResolver addressFromString: '255.255.255.255'. sourcePort := 4848. buffer := ByteArray new: 1000. delay := Delay forMilliseconds: 50. socket := Socket newUDP setPort: localPort. [[ receivedBytes := socket receiveDataInto: buffer fromHost: sourceAddress port: sourcePort. receivedBytes isZero ] whileTrue: [ delay wait ]] ensure: [ socket closeAndDestroy]. Or you are better off testing it by using #receiveDataInto: instead of #receiveDataInto:fromHost:port: Norbert
On Feb 6, 2012, at 1:38 PM, jannik.laval wrote:
Hi Todd,
The socket chapter in writing progress could help you: https://gforge.inria.fr/scm/viewvc.php/*checkout*/PharoByExampleTwo-Eng/Sock... I do not remember if the receiveData method is blocking. If not, you should wait til data arrives.
Cheers, Jannik
On Feb 6, 2012, at 22:22 , Eagle Offshore wrote:
I'm trying to build a display for some instruments that multicast readings using UDP. I started out using CocoaAsyncSocket on Mac (https://github.com/robbiehanson/CocoaAsyncSocket) and I have to say it has a really wonderful api - would be worth cloning it.
However, want to do the same thing in Pharo so I have typed this little snippet into the workspace:
[| socket | socket := Socket newUDP. 10 timesRepeat: [| s | s := String new. socket receiveDataInto: s fromHost: (NetNameResolver addressFromString:'255.255.255.255' ) port: 4848. Transcript show: s.]] fork
and it doesn't seem to do anything.
What have I missed? Pharo 1.3 stable on SnowLeopard BTW.
Thanks, -Todd Blanchard
--- Jannik Laval
Todd, On 06 Feb 2012, at 22:22, Eagle Offshore wrote:
[| socket | socket := Socket newUDP. 10 timesRepeat: [| s | s := String new. socket receiveDataInto: s fromHost: (NetNameResolver addressFromString:'255.255.255.255' ) port: 4848. Transcript show: s.]] fork
and it doesn't seem to do anything.
What have I missed?
It is a non-blocking API, so you have to write some code to keep on listening until some timeout, in a loop with small delays, like 50ms. Also, you have to preallocate a non-empty buffer. I have some working code for this, but it is not clean/standalone enough to show here ;-) If you can't figure it out yourself, just ask and I will mail some of it to you off list. Regards, Sven
participants (4)
-
Eagle Offshore -
jannik.laval -
Norbert Hartl -
Sven Van Caekenberghe