Platforms to show: All Mac Windows Linux Cross-Platform
/Network/Raw Socket/Unix Socket doing UDP
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/Raw Socket/Unix Socket doing UDP
This example is the version from Sun, 23th Sep 2017.
Project "Unix Socket doing UDP.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control iMessage Inherits TextField
ControlInstance iMessage Inherits TextField
EventHandler Sub TextChange()
SendButton.Enabled = me.text.len > 0
End EventHandler
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control iIP Inherits TextField
ControlInstance iIP Inherits TextField
End Control
Control Label3 Inherits Label
ControlInstance Label3 Inherits Label
End Control
Control iPort Inherits TextField
ControlInstance iPort Inherits TextField
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control SendButton Inherits PushButton
ControlInstance SendButton Inherits PushButton
EventHandler Sub Action()
dim Flags as integer
dim r as Integer
// port
dim port as integer = val(iPort.Text)
if port <= 0 or port >= 65535 then
break
MsgBox "Wrong port: "+iPort.Text
Return
end if
// dest address
'/*
'* Socket address, internet style. C declaration for Mac
'*/
'struct sockaddr_in {
'__uint8_t sin_len; // 1 byte, Mac only
'sa_family_t sin_family; // 1 byte
'in_port_t sin_port; // 2 byte in Network byte order
'struct in_addr sin_addr; // 4 byte
'char sin_zero[8]; // 8 bytes filler
'};
// convert IPv4 from text to numeric value
dim IP as Uint32 = RAWSocketMBS.inet_addr(iIP.Text)
// create destination address. This is for Mac! Windows and Linux have different structures
dim dest as new MemoryBlock(16)
#if TargetMacOS
dest.UInt8Value(0) = dest.size
dest.UInt8Value(1) = RAWSocketMBS.AddressFamilyINet
dest.UInt16Value(2) = RAWSocketMBS.htons(port)
dest.UInt32Value(4) = IP
#else
dest.UInt8Value(0) = RAWSocketMBS.AddressFamilyINet
dest.UInt16Value(2) = RAWSocketMBS.htons(port)
dest.UInt32Value(4) = IP
#endif
// message
dim m as string = ConvertEncoding(iMessage.Text, encodings.UTF8)
dim data as MemoryBlock = m
// send
r = sock.SendTo(data, data.size, Flags, dest, dest.size)
dim SIP as string = RAWSocketMBS.inet_ntoa(dest.UInt32Value(4))
dim SPort as string = str(RAWSocketMBS.ntohs(dest.UInt16Value(2)))
log "Sent "+str(r)+" bytes to "+SIP+":"+SPort
iMessage.Text = ""
End EventHandler
End Control
EventHandler Sub Close()
sock = nil
End EventHandler
EventHandler Sub Open()
sock = new MyRawSocket(RAWSocketMBS.AddressFamilyINet, RAWSocketMBS.SocketTypeDatagram, RAWSocketMBS.ProtocolUDP)
// to test receive, we bind to any address with given port
// convert IPv4 from text to numeric value
dim IP as Uint32 = 0 // all IPv4
// create destination address. This is for Mac! Windows and Linux have different structures
dim dest as new MemoryBlock(16)
#if TargetMacOS then
dest.UInt8Value(0) = dest.size
dest.UInt8Value(1) = RAWSocketMBS.AddressFamilyINet
dest.UInt16Value(2) = RAWSocketMBS.htons(12345)
dest.UInt32Value(4) = IP
#else
dest.UInt8Value(0) = RAWSocketMBS.AddressFamilyINet
dest.UInt16Value(2) = RAWSocketMBS.htons(12345)
dest.UInt32Value(4) = IP
#endif
if sock.Bind(dest, dest.size) then
log "Bind OK"
else
log "Bind: "+str(sock.Lasterror)
end if
End EventHandler
Sub log(s as string)
List.AddRow s
list.ScrollPosition = list.ScrollPosition + 1
End Sub
Property sock As MyRawSocket
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyRawSocket Inherits RAWSocketMBS
EventHandler Sub DataAvailable()
log CurrentMethodName
// simply log what we read
if false then
// using ReadAll
dim m as MemoryBlock = me.ReadAll
dim s as string = DefineEncoding(m, encodings.UTF8)
log s
else
// using ReadDatagram
dim d as DatagramMBS = me.ReadDatagram
dim s as string = DefineEncoding(d.Data, encodings.UTF8)
log "Received: "+s
log "from "+d.Address+":"+str(d.Port)
end if
End EventHandler
EventHandler Sub Error()
log CurrentMethodName
End EventHandler
EventHandler Sub SendComplete()
log CurrentMethodName
End EventHandler
Sub log(s as string)
MainWindow.log s
End Sub
End Class
End Project
See also:
The items on this page are in the following plugins: MBS Network Plugin.