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 := '' asMSKSciSocketAddress.
helloGroup port: 21342.

recInstance := MSKMulticastReceiver newWithInterfaceAddress: localAddress.

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

"we invite the receiver to our group"
 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"

"now the sender part"
sndInstance := MSKMulticastSender newWithGroupAddress: helloGroup.
(anError := sndInstance connect) isSciError
    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.
This entry was posted in Smalltalk. Bookmark the permalink.