Platforms to show: All Mac Windows Linux Cross-Platform

/Network/UDP Socket/UDPSocketMBS test


Required plugins for this example: MBS Network Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Network/UDP Socket/UDPSocketMBS test

This example is the version from Sun, 22th Aug 2015.

Project "UDPSocketMBS test.xojo_binary_project"
Class MainWindow Inherits Window
Const kGroupAddress = "225.1.2.3"
Const kPort = 8345
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control MessageField Inherits TextField
ControlInstance MessageField Inherits TextField
End Control
Control SendButton Inherits PushButton
ControlInstance SendButton Inherits PushButton
EventHandler Sub Action() call UdPSocket1.SendMessage(MessageField.Text, AddressField.Text, kPort) AddMessage("Sending to " + AddressField.Text + ": " + MessageField.Text) End EventHandler
End Control
Control MessageList Inherits ListBox
ControlInstance MessageList Inherits ListBox
End Control
Control UDPSocket1 Inherits UDPSocketMBS
ControlInstance UDPSocket1 Inherits UDPSocketMBS
EventHandler Sub DataAvailable() Dim d As DatagramMBS = UDPSocket1.Read ShowMessage d End EventHandler
EventHandler Sub Error() dim e as integer = me.SocketError AddMessage("Error: " + str(e)) End EventHandler
EventHandler Sub SendComplete() AddMessage("Send complete.") End EventHandler
End Control
Control SendMulticastButton Inherits PushButton
ControlInstance SendMulticastButton Inherits PushButton
EventHandler Sub Action() AddMessage("Sending to all in group: " + MessageField.Text) call UdPSocket1.SendMessage(MessageField.Text, kGroupAddress, kPort) End EventHandler
End Control
Control AddressField Inherits TextField
ControlInstance AddressField Inherits TextField
EventHandler Sub Open() dim u as new UDPSocket me.Text = u.LocalAddress End EventHandler
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control BroadcastIPButton Inherits PushButton
ControlInstance BroadcastIPButton Inherits PushButton
EventHandler Sub Action() dim u as new UDPSocket Dim broadcastAddress As String = u.BroadcastAddress if broadcastAddress = "" then MsgBox "no broadcast address?" Return end if System.DebugLog broadcastAddress dim n as integer = UDPSocket1.SendMessage(MessageField.Text, broadcastAddress, kPort) AddMessage("Sending "+str(n)+" bytes to all ("+broadcastAddress+"): " + MessageField.Text) End EventHandler
End Control
Control SendToSelfCheck Inherits CheckBox
ControlInstance SendToSelfCheck Inherits CheckBox
EventHandler Sub Action() UDPSocket1.MulticastLoop = Me.Value End EventHandler
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control GroupBox1 Inherits GroupBox
ControlInstance GroupBox1 Inherits GroupBox
End Control
Control BindPortButton Inherits PushButton
ControlInstance BindPortButton Inherits PushButton
EventHandler Sub Action() UDPSocket1.ReuseAddress = true if UDPSocket1.Lasterror <> 0 then MsgBox "Failed to ask for ReuseAddress." end if UDPSocket1.ReusePort = true if UDPSocket1.Lasterror <> 0 then MsgBox "Failed to ask for ReusePort." end if dim Port as integer = Val(PortField.Text) UDPSocket1.Bind(port) // bind to all addresses if UDPSocket1.Lasterror = 0 then AddMessage("Bound to port: " + PortField.Text+ " for all addresses") else UDPSocket1.Bind(port, LocalAddress) // bind only to local address if UDPSocket1.Lasterror = 0 then AddMessage("Bound to port: " + PortField.Text+ " for IP " + LocalAddress) else AddMessage("Failed to bind to IP "+LocalAddress+" to port: " + PortField.Text+" with error: "+str(UDPSocket1.Lasterror)) end if end if End EventHandler
End Control
Control StaticText2 Inherits Label
ControlInstance StaticText2 Inherits Label
End Control
Control PortField Inherits TextField
ControlInstance PortField Inherits TextField
EventHandler Sub Open() Me.Text = str(kPort) End EventHandler
End Control
Control StaticText3 Inherits Label
ControlInstance StaticText3 Inherits Label
End Control
Control TTLField Inherits TextField
ControlInstance TTLField Inherits TextField
EventHandler Sub Open() Me.Text = Str(UDPSocket1.TimeToLive) End EventHandler
End Control
Control SetTTLButton Inherits PushButton
ControlInstance SetTTLButton Inherits PushButton
EventHandler Sub Action() UDPSocket1.TimeToLive = Val(TTLField.Text) AddMessage("Set TTL: " + TTLField.Text) End EventHandler
End Control
Control GroupBox2 Inherits GroupBox
ControlInstance GroupBox2 Inherits GroupBox
End Control
Control JoinButton Inherits PushButton
ControlInstance JoinButton Inherits PushButton
EventHandler Sub Action() UDPSocket1.AddMembership(kGroupAddress) If UDPSocket1.Lasterror = 0 Then AddMessage("Joined the UDP Group: " + kGroupAddress) Else AddMessage("Failed to join the UDP Group: " + kGroupAddress) End End EventHandler
End Control
Control LeaveButton Inherits PushButton
ControlInstance LeaveButton Inherits PushButton
EventHandler Sub Action() UDPSocket1.DropMembership(kGroupAddress) AddMessage("Left the UDP Group: " + kGroupAddress) End EventHandler
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() AddMessage "Availablebytes: "+str(UDPSocket1.AvailableBytes) End EventHandler
End Control
Control ReadButton Inherits PushButton
ControlInstance ReadButton Inherits PushButton
EventHandler Sub Action() Dim d As DatagramMBS = UDPSocket1.Read ShowMessage d End EventHandler
End Control
EventHandler Sub Open() 'UDPSocket1.ReuseAddress = true 'if UDPSocket1.Lasterror <> 0 then 'MsgBox "Failed to ask for broadcast." 'end if UDPSocket1.Broadcast = true if UDPSocket1.Lasterror <> 0 then MsgBox "Failed to ask for broadcast." end if UDPSocket1.MulticastLoop = true if UDPSocket1.Lasterror <> 0 then MsgBox "Failed to ask for multicast loop." end if MessageField.SetFocus dim u as new UDPSocket localAddress = u.LocalAddress Self.Title = "UDP Example MBS (" + LocalAddress + ")" End EventHandler
Private Sub AddMessage(text As String) MessageList.InsertRow(0, text) End Sub
Private Sub ShowMessage(d as DatagramMBS) if d<>Nil then dim s as string = d.Data // fix encoding if unknown if s.Encoding = nil then if Encodings.UTF8.IsValidData(s) then s = DefineEncoding(s, encodings.UTF8) else // fall back to something s = DefineEncoding(s, encodings.ISOLatin1) end if end if AddMessage("Received message: " + s) else AddMessage("Received no message") end if End Sub
Property LocalAddress As string
End Class
MenuBar MainMenuBar
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
EventHandler Function UnhandledException(error As RuntimeException) As Boolean MsgBox Introspection.GetType(error).name+EndOfLine+EndOfLine+error.Message+EndOfLine+Join(error.Stack, EndOfLine) Return true End EventHandler
End Class
End Project

The items on this page are in the following plugins: MBS Network Plugin.


The biggest plugin in space...