Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Mar 28, 2017 8:18 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: TCP Socket - nonexisting IP problem
PostPosted: Tue Feb 05, 2013 5:52 am 
Offline

Joined: Tue Oct 30, 2012 4:23 am
Posts: 22
Hello Network experts,

I need check possibility of connect before running some code on TCP SOcket.
I use RCP socket example from reference, which works well on IPs which are available.
I got into problem, when I try to connect to nonexisting IP.

My Code:
Function TCP_Connect(xIP as string, xPort as integer) As Boolean
Dim start, stop as Integer
Dim conn as Boolean
Dim TCPSocket1 as TCPSocket
TCPSocket1 = New TCPSocket
TCPSocket1.Address = xIP
TCPSocket1.Port = xPort
TCPSocket1.Connect
start = Ticks
While Not TCPSocket1.IsConnected
If TCPSocket1.LastErrorCode <> 0 then
StatusBar.text = "Socket Error: " + str(TCPSocket1.LastErrorCode)
Exit

End If
//poll the socket to let it do its thing
TCPSocket1.Poll

Wend
stop = Ticks

//if we broke the loop because we're connected
If TCPSocket1.IsConnected then
GuardWindow.StatusBar.text= "Socket Connected in " + Str(stop - start) + " ticks"
//here would be a great place to do a synchronous read operation...
conn = true
Else
//The socket broke out of the loop due to an error
GuardWindow.StatusBar.text= "Socket failed to connect. It took " + Str(stop - start) + _
" ticks to figure that out"
conn= false
End If

TCPSocket1.Close
GuardWindow.StatusBar.text= "Socket closed"

return conn
End Function

_________________
---

@MacOS 10.7.5/MBPro


Top
 Profile  
 
 Post subject: Re: TCP Socket - nonexisting IP problem
PostPosted: Tue Feb 05, 2013 10:34 am 
Offline
User avatar

Joined: Sat Nov 11, 2006 2:43 pm
Posts: 1221
Location: This poster has left the forums
duplicate post, see my response to you other post


Add a timeout value after which to give up.
NOTE - Ticks can roll over, so add some error checking

Function TCP_Connect(xIP as string, xPort as integer) As Boolean
Dim start, stop as Integer
Dim conn as Boolean
Dim TCPSocket1 as TCPSocket
TCPSocket1 = New TCPSocket
TCPSocket1.Address = xIP
TCPSocket1.Port = xPort
TCPSocket1.Connect
start = Ticks
dim timeout as Integer = 180 //3 seconds
While Not TCPSocket1.IsConnected
If TCPSocket1.LastErrorCode <> 0 then
StatusBar.text = "Socket Error: " + str(TCPSocket1.LastErrorCode)
Exit
End If
TCPSocket1.Poll
if Ticks > start +timeout then //did 3 seconds elapse
StatusBar.text= "Timed out" //we time out, dang!
conn = false//no connection
exit//exit the loop
end
Wend
stop = Ticks
If TCPSocket1.IsConnected then
StatusBar.text= "Socket Connected in " + Str(stop - start) + " ticks"
conn = true
Else
StatusBar.text= "Socket failed to connect. It took " + Str(stop - start) + _
" ticks to figure that out"
conn= false
End If
TCPSocket1.Close
StatusBar.text= "Socket closed"
return conn
End Function

_________________
%Invalidforumsignatureexception% user signature not found


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group