Real Software Forums
http://forums.realsoftware.com/

TCP Socket - nonexisting IP problem
http://forums.realsoftware.com/viewtopic.php?f=2&t=46822
Page 1 of 1

Author:  mapmusic [ Tue Feb 05, 2013 6:15 am ]
Post subject:  TCP Socket - nonexisting IP problem

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

Author:  pony [ Tue Feb 05, 2013 10:31 am ]
Post subject:  Re: TCP Socket - nonexisting IP problem

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

Author:  mapmusic [ Tue Feb 05, 2013 10:56 am ]
Post subject:  Re: TCP Socket - nonexisting IP problem

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.

Author:  pony [ Tue Feb 05, 2013 11:17 am ]
Post subject:  Re: TCP Socket - nonexisting IP problem

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.

Author:  mapmusic [ Wed Feb 06, 2013 2:33 am ]
Post subject:  Re: TCP Socket - nonexisting IP problem

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.

Author:  msssltd [ Wed Feb 06, 2013 3:48 am ]
Post subject:  Re: TCP Socket - nonexisting IP problem

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

Author:  mapmusic [ Wed Feb 06, 2013 5:30 am ]
Post subject:  Re: TCP Socket - nonexisting IP problem

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 !

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/