Platforms to show: All Mac Windows Linux Cross-Platform
/Network/DNS Lookup with Timeout
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/DNS Lookup with Timeout
This example is the version from Thu, 20th Jun 2012.
Project "DNS Lookup with Timeout.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control ProgressWheel1 Inherits ProgressWheel
ControlInstance ProgressWheel1 Inherits ProgressWheel
End Control
Control ProgressBar1 Inherits ProgressBar
ControlInstance ProgressBar1 Inherits ProgressBar
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control Timer1 Inherits Timer
ControlInstance Timer1 Inherits Timer
EventHandler Sub Action()
Counter = counter + 1
dim t as integer = ticks
if LastTicks<>t and t mod 100 = 0 then
dim DeltaCounter as integer = counter-LastCounter
dim deltaTicks as integer = t - LastTicks
label2.text = Format(DeltaCounter*60/deltaTicks,"0.0")+" per second"
if instr(Label2.Text, "INF")>0 then
break
end if
LastTicks = t
LastCounter = counter
end if
End EventHandler
End Control
Control Label3 Inherits Label
ControlInstance Label3 Inherits Label
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action()
dim d as Double = Microseconds
List.DeleteAllRows
dim s as string = TextField1.text
dim a as string = System.Network.LookupIPAddress(s)
if a <> "" then
List.AddRow "IP: "+a
else
List.AddRow "Lookup failed."
end if
d = Microseconds-d
List.AddRow Format(d/1000000.0, "0.00")+" seconds"
End EventHandler
End Control
Control TextField1 Inherits TextField
ControlInstance TextField1 Inherits TextField
End Control
Control Label4 Inherits Label
ControlInstance Label4 Inherits Label
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action()
dim d as Double = Microseconds
List.DeleteAllRows
dim s as DNSLookupMBS = DNSLookupMBS.LookupHostbyNameMT(TextField1.Text, DNSLookupMBS.AddressTypeIPv4)
if s<>nil then
dim c as integer = s.AddressesCount-1
for i as integer = 0 to c
dim ss as string = s.Addresses(i)
List.AddRow "Address: "+str(s.FormatIP(ss))
next
else
List.AddRow "Lookup failed."
end if
d = Microseconds-d
List.AddRow Format(d/1000000.0, "0.00")+" seconds"
End EventHandler
End Control
Control PushButton3 Inherits PushButton
ControlInstance PushButton3 Inherits PushButton
EventHandler Sub Action()
dim d as Double = Microseconds
List.DeleteAllRows
dim t as new LookupThread
t.query = TextField1.text
t.run
// now we wait here 3 seconds for answer
dim ti as integer = ticks + 180
while t.done = false and ti>ticks
app.DoEvents 10
wend
dim s as DNSLookupMBS = t.result
if s<>nil then
dim c as integer = s.AddressesCount-1
for i as integer = 0 to c
dim ss as string = s.Addresses(i)
List.AddRow "Address: "+str(s.FormatIP(ss))
next
else
List.AddRow "Lookup failed due timeout."
end if
d = Microseconds-d
List.AddRow Format(d/1000000.0, "0.00")+" seconds"
End EventHandler
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control PushButton4 Inherits PushButton
ControlInstance PushButton4 Inherits PushButton
EventHandler Sub Action()
List.DeleteAllRows
List.AddRow "IsConnected: "+str(System.Network.IsConnected)
End EventHandler
End Control
Control Label5 Inherits Label
ControlInstance Label5 Inherits Label
End Control
Control Label6 Inherits Label
ControlInstance Label6 Inherits Label
End Control
EventHandler Sub Open()
LastCounter = 0
LastTicks = ticks
End EventHandler
Property Counter As Integer
Property LastCounter As Integer
Property LastTicks As Integer
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class LookupThread Inherits Thread
EventHandler Sub Run()
startTicks = ticks
System.DebugLog "Lookup in thread: "+query+" at "+str(startTicks)
result = DNSLookupMBS.LookupHostbyNameMT(query, DNSLookupMBS.AddressTypeIPv4)
done = True
System.DebugLog "Lookup in thread: "+query+" end at "+str(ticks)
if result<>Nil then
System.DebugLog "Thread got result after "+Format((ticks-startticks)/60.0,"0.0")+" seconds"
dim c as integer = result.AddressesCount-1
for i as integer = 0 to c
dim ss as string = result.Addresses(i)
System.DebugLog "Address: "+str(result.FormatIP(ss))
next
else
System.DebugLog "Thread got no result after "+Format((ticks-startticks)/60.0,"0.0")+" seconds"
end if
End EventHandler
Property done As Boolean
Property query As string
Property result As DNSLookupMBS
Property startticks As Integer
End Class
End Project
See also:
The items on this page are in the following plugins: MBS Network Plugin.