Timeout for OpenNetworkConnection

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Timeout for OpenNetworkConnection

Post by Marlin »

Connection = OpenNetworkConnection(ServerName$, Port [, Mode[, Timeout]])
Wenn trying to connect by tcp to a http server, that appeared to be down,
I found that OpenNetworkConnection() took about 5 minutes to return.
= Program execution halts for about 5 minutes
(I used a Linux machine to try that connect.)

When the server is availlable, it takes only a few,
but allways less than 20 seconds to open
the network connection.
(Same conditions on the client side)
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Re: Timeout for OpenNetworkConnection

Post by Num3 »

I use this code, for a quicker check:

Code: Select all

Structure myEcho
  Reply.ICMP_ECHO_REPLY
  Buffer.l[20]
EndStructure

ping.myEcho

server.s = "192.168.1.1"

tOut.l = 5 ; <- TIME OUT

hP = IcmpCreateFile_()
If hP
  con.i = IcmpSendEcho_(hP, inet_addr_(@server), @"HI", Len("HI"), 0, @ping, SizeOf(myecho), tOut)
  IcmpCloseHandle_(hP)
EndIf

If con = 0 ; Connection to 192.168.1.1 failed !
  ; Do something here
EndIf
Marlin
Enthusiast
Enthusiast
Posts: 406
Joined: Sun Sep 17, 2006 1:24 pm
Location: Germany

Re: Timeout for OpenNetworkConnection

Post by Marlin »

Num3 wrote:I use this code, for a quicker check:
...
Your code seems to be specific for some type of platform (Windows?)

I'd rather prefer simple solutions in native PB code.

I came up with this solution with the use of threading.
(That should work on all supported platforms; I only tried it on my Linux machine yet.)

However my above suggestion looks much nicer and simpler,
and is probably much more appealing to anyone considering
the use of such a feature. ;-)
Post Reply