Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Dec 11, 2017 8:20 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: TCP Socket - nonexisting IP problem
PostPosted: Tue Feb 05, 2013 6:15 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 TCP socket example from Reference Guide, which works well on IPs which are available.
I got into problem, when I try to connect to nonexisting IP.
Problem is when I am connecting there, in "While" part I have
IsConnetcted= False
and
LastErrorCode=0
, so code is cycling .
Have you any tip to check this in code ?

Thanks

Martin

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
TCPSocket1.Poll
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

_________________
---

@MacOS 10.7.5/MBPro


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

Joined: Sat Nov 11, 2006 2:43 pm
Posts: 1221
Location: This poster has left the forums
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  
Reply with quote  
 Post subject: Re: TCP Socket - nonexisting IP problem
PostPosted: Tue Feb 05, 2013 10:56 am 
Offline

Joined: Tue Oct 30, 2012 4:23 am
Posts: 22
Thanks pony,

It is good idea to check for connection only for 3 seconds. I will implement 8)

But Is there any way to check IP by ping it ?
Maybe it could be better before opening any socket.

Thanks

M.

_________________
---

@MacOS 10.7.5/MBPro


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket - nonexisting IP problem
PostPosted: Tue Feb 05, 2013 11:17 am 
Offline
User avatar

Joined: Sat Nov 11, 2006 2:43 pm
Posts: 1221
Location: This poster has left the forums
mapmusic wrote:
Thanks pony,

It is good idea to check for connection only for 3 seconds. I will implement 8)

But Is there any way to check IP by ping it ?
Maybe it could be better before opening any socket.

Thanks

M.


You could spawn a shell and issue a ping.

Not all devices answer to ICMP pings.

I don't know what you are connecting to, but I can envisage an instance were a remote IP answered to a ping, but did not have the service you want running on it (ping a web server, but Apache or IIS etc isnt running).
For my network applications, I'm not interested if they are replying to a ping or not. I'm only interested in the service I wish to connect to.

_________________
%Invalidforumsignatureexception% user signature not found


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket - nonexisting IP problem
PostPosted: Wed Feb 06, 2013 2:33 am 
Offline

Joined: Tue Oct 30, 2012 4:23 am
Posts: 22
I agree, that ping is not way to check if service is working.
(I am connecting to software with TCP services on 2 ports and I can ping this software)
But I am finding way to avoid 2 subroutines (each 3 seconds) if not needed.

Anyway thanks for tips.

M.

_________________
---

@MacOS 10.7.5/MBPro


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket - nonexisting IP problem
PostPosted: Wed Feb 06, 2013 3:48 am 
Offline
User avatar

Joined: Fri Oct 28, 2005 7:05 am
Posts: 565
Location: Emsworth, UK
Does this help?

Sub Form1.Button1.Action
TCPSocket1.Address = "localhost"
TCPSocket1.Port = 1023

TCPSocket2.Address = "localhost"
TCPSocket2.Port = 1024

//Fail early.
Dim fSuccess as boolean
If TCPSocket1.Connect(3000) Then
If TCPSocket2.Connect(3000) then
fSuccess = true
End if
End if

If not fSuccess then
// .IsConnected can tells us which socket failed
// .LastErrorCode can tell us what went wrong
Return
End if

//All connected up.
End Sub

//Add a module to your project and add the following method
Function Connect(extends tcp as TCPSocket, time_out_ms as integer) as boolean
Dim time_out as double = time_out_ms * 1000
Dim start_ms as double = Microseconds

tcp.Connect
Do
tcp.Poll
Loop until tcp.IsConnected or (Microseconds - start_ms > time_out)

Return tcp.IsConnected
End Function

_________________
Yes it's me in the avatar


Top
 Profile  
Reply with quote  
 Post subject: Re: TCP Socket - nonexisting IP problem
PostPosted: Wed Feb 06, 2013 5:30 am 
Offline

Joined: Tue Oct 30, 2012 4:23 am
Posts: 22
Thanks msssltd,

Tip from pony also worked fine, but I used your modified code. (Extends is cool ... it is new for me 8) )

Thanks guys for solve my problem !

_________________
---

@MacOS 10.7.5/MBPro


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

All times are UTC - 5 hours


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