Multicast transmission under VASmalltalk

After working with C# program sending/receiving information via multicast udp datagrams I had the wish to be able to receive and send datagrams also via Smalltalk.

I published a source code package at MSKUDPMulticast, where code for receiving and sending is included.

The source is – at this time – only running under Windows, due to some specific Windows values.

| recInstance sndInstance helloGroup localAddress anError  |

"we define the local interfaces, where we want to hear/send"
localAddress := INADDRANY asMSKSciSocketAddress.
localAddress port: 21342.

"we define the multicast group - our group home"
helloGroup := '224.0.0.1' asMSKSciSocketAddress.
helloGroup port: 21342.

recInstance := MSKMulticastReceiver newWithInterfaceAddress: localAddress.

"binding the receiving socket"
(anError := recInstance bindReceivingSocket) isSciError
  ifTrue:[
     recInstance close.
     ^Transcript cr ; show: 'Error creating instance ' ].

"we invite the receiver to our group"
recInstance
 addMulticastMembershipFor: helloGroup;

 "what do we do with receiving datagrams"
 forEachDatagramExecute: [ :receiver :byteArray :size |
                                           Transcript cr ; show: (byteArray asMSKStringWithSize: size) ]
 withReceiver: recInstance ;

  "and start the work"
  startWork.

"now the sender part"
sndInstance := MSKMulticastSender newWithGroupAddress: helloGroup.
(anError := sndInstance connect) isSciError
  ifTrue:[
    sndInstance close.
    recInstance close.
    ^Transcript cr ; show: 'Error creating instance ' ].

sndInstance startWork.

"now we open two inspectors - that you can work with these instances. to finish this example, just send 'close' to both instances"
recInstance inspect.
sndInstance inspect.
Advertisement
This entry was posted in Smalltalk. Bookmark the permalink.