TCP or UDP timeout

BlitzMax Forums/BlitzMax Beginners Area/TCP or UDP timeout

Moogles(Posted 2006) [#1]
Why does this code not work?
Framework BRL.Socket
Import BRL.System
Import BRL.SocketStream
Import BRL.StandardIO

Function Connect(serverIP$,port,timeout)
	Local serverIPN = IPtoInt(serverIP$)
	Local socket=CreateTCPSocket()
	Local connect=ConnectSocket(socket,serverIPN,port)
	Local starttime=MilliSecs()
	
	Repeat 
		endtime=MilliSecs()
		passedtime=(endtime-starttime)
	Until passedtime >= timeout Or connect
	
	If passedtime >= timeout
		connect=2
	Else	
		Local stream=CreateSocketStream(socket)
	EndIf
	
	Return connect
	
	Function IPtoInt(serverIP$)
		serverIP$:+"."
		Local sect:String[4]
		For i = 0 To 3
			part =  serverIP$.find(".") 
			sect[i] =  serverIP$[0..part]
			serverIP$ = serverIP$[part+1..]
		Next
	Return (sect[0].toint() Shl 24) + (sect[1].toint() Shl 16) + (sect[2].toint() Shl 8) + sect[3].toint()
	End Function
	
End Function


time=MilliSecs()
out=connect("82.2.199.245",80,1000)
Print out
timepassed=MilliSecs()-time
Print timepassed


As you can see I set my own timeout but instead out is printed 20 seconds later after i call my connect function. I notice also that if i use udp it will say its connected even if the ip cannot be reached. is this a bug?


Dreamora(Posted 2006) [#2]
Your own timeout makes no difference.
If you put a print "Test" after the ConnectSocket line you will realize that this one is the function that defines the timeout. Its internal.
The only thing your repeat does afterwards is simply running once and thats it.

If you want your own timeout on connection you need to do you own ConnectSocket function.


Moogles(Posted 2006) [#3]
...
i thought that was it. this should be implemented. although i dont know how.
btw quick response. i was beginning to think theres no one here. want to try explaining why more other post doesnt work?
http://www.blitzmax.com/Community/posts.php?topic=57378

Oh my timeout works now. :)
well if its the right ip address my timeout will work. otherwise it will loop for 20 seconds. IMO BlitzMax should have been made with PHP functions. They're good and they have options. I know for creating files they use php funcs but why not for sockets and stuff to do with ip's
And this Still doesnt explain why UDP connects and says its connected when the port isnt open.
Ill try posting in the bug section. No one seems to have noticed this.