Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagevb
titleExample
 ' UDP 2-way peer-to-peer asynchronous comm on port 54321
 ' periodically sends out a message to a specific address and port
 ' prints any message it receives 
 Function UDPPeer()
     msgPort = createobject("roMessagePort") 
     udp = createobject("roDatagramSocket")
     udp.setMessagePort(msgPort) 'notifications for udp come to msgPort
     addr = createobject("roSocketAddress")
     addr.setPort(54321)
     udp.setAddress(addr) ' bind to all host addresses on port 54321
     addr.SetHostName("10.1.1.1")
     udp.setSendToAddress(addr) ' peer IP and port
     udp.notifyReadable(true) 
     timeout = 1 * 10 * 1000 ' ten seconds in milliseconds
     uniqueDevdeviceName = createobjectCreateobject("roDeviceInfo").GetDeviceUniqueIdGetFriendlyName()
     message = "Datagram from " + uniqueDevdeviceName
     udp.sendStr(message)
     continue = udp.eOK() 
     While continue
         event = wait(timeout, msgPort)
         If type(event)="roSocketEvent"
             If event.getSocketID()=udp.getID()
                 If udp.isReadable()
                     message = udp.receiveStr(512) ' max 512 characters
                     print "Received message: '"; message; "'"
                 End If
             End If
         Else If event=invalid
             print "Timeout"
             udp.sendStr(message) ' periodic send
         End If
     End While 
     udp.close() ' would happen automatically as udp goes out of scope
 End Function 

...