Thanks sven

I will check with the guys that wrote the socket chapter if we can add this snippet.

Stef

On 20/10/14 23:44, Sven Van Caekenberghe wrote:
Again, Pharo is a high-level language, not a low-level one.

Here is a working, standalone example:

"An UDP echo server on port 6666"
[ Socket newUDP in: [ :socket |
   | loop buffer result input |
   buffer := String new: 256.
   loop := true.
   socket setPort: 6666.
   [ loop ] whileTrue: [
     [ result := socket receiveUDPDataInto: buffer.
       result first > 0 ] whileFalse: [ (Delay forMilliseconds: 10) wait ].
     input := buffer copyFrom: 1 to: result first.
     socket sendUDPData: input toHost: result second port: result third.
     (input beginsWith: #quit) ifTrue: [ loop := false ] ].
     socket closeAndDestroy ] ] forkAt: Processor userBackgroundPriority named: 
'UDP echo server'.

"Any message sent gets echoed back"
Socket newUDP in: [ :socket |
   | buffer result |
   socket sendUDPData: 'testing ', 99 atRandom asString toHost: NetNameResolver 
localHostAddress port: 6666.
   buffer := String new: 256.
   [ result := socket receiveUDPDataInto: buffer.
     result first > 0 ] whileFalse: [ (Delay forMilliseconds: 10) wait ].
   socket closeAndDestroy.
   { result. buffer. buffer copyFrom: 1 to: result first } ].

"Send quit to stop the server"
Socket newUDP in: [ :socket |
   | buffer result |
   socket sendUDPData: 'quit' toHost: NetNameResolver localHostAddress port: 
6666.
   buffer := String new: 256.
   [ result := socket receiveUDPDataInto: buffer.
     result first > 0 ] whileFalse: [ (Delay forMilliseconds: 10) wait ].
   socket closeAndDestroy.
   { result. buffer. buffer copyFrom: 1 to: result first } ].

In a terminal (OSX or Linux), you can send a message like this:

$ nc -u 127.0.0.1 6666
foo bar
foo bar
quit
quit

HTH,

Sven

On 20 Oct 2014, at 22:39, Annick Fron <l...@afceurope.com> wrote:

In your answer you mention 1 2 3 4 data, without specifying how many bytes they 
have.
Typically the IP address needs 4 bytes, so your answer 1 2 3 4 is quite 
incomplete.
When I receive data like that

[28 0 203 204 …….]

I don’t know if I have only the datagram or if I have also the IP header.
So my question is how do you group the bytes to get the IP address ???
Where do the data start ? What headers do I have and with which syntax ????

Annick


Le 20 oct. 2014 à 17:32, Sven Van Caekenberghe <s...@stfx.eu> a écrit :

I don't understand why you would need it, see my previous reply to your other 
message.

On 20 Oct 2014, at 17:23, Annick Fron <l...@afceurope.com> wrote:

Hi,

Is it possible to get the documentation of the socket api signature, because 
otherwise I am unable to setup a UDP server ?

Annick






Reply via email to