TCP Socket Connection

BlitzMax Forums/BlitzMax Programming/TCP Socket Connection

Ked(Posted 2008) [#1]
When I try to connect to an IP and port of a computer that is not running the server, it takes forever for it to tell me that it cannot connect. Is it possible to speed this up without having to leave the BlitzMax's TCP command set or without having to go to a networking lib?

Thanks,


Winni(Posted 2008) [#2]
It doesn't look like it, and BlitzMax does not have the option of running such tasks in separate threads either (where blocking I/O wouldn't matter).

By the way, the timeout on OS X Leopard seems to be 1 minute 15 seconds - that could be a lot worse. :-p


Ked(Posted 2008) [#3]
Ok. Thanks, Winni.


Vertex(Posted 2008) [#4]
You have to set the socket in non-blocking mode and then use after connect the select command to determine the connection.

cu olli


Winni(Posted 2008) [#5]
Vertex, yes, that is the correct approach towards Multiplexing. But how do you do that with the default set of BlitzMax functions?


Vertex(Posted 2008) [#6]
You can do this without C-Code. I implement this feature in my BNetEx module. On Windows it looks like:
' Set socket to Non-Blocking mode
Local option : Int
option = True
ioctlsocket(socket, FIONBIO, Varptr(option))

' Connect
If connect(socket, address) = SOCKET_ERROR Then
	If WSAGetLastError() <> WSAEWOULDBLOCK Then
		' Any other error
	EndIf
EndIf

' Check state
Local write : int
write = socket
Select select(0, Null, 1, Varptr(write), 0, Null, timeout)
	Case SOCKET_ERROR
		' Any other error
	Case 0
		' Server is not reachable
	Default
		' Connected to server
End Select

' Go into Blocking mode
option = False
ioctlsocket(socket, FIONBIO, Varptr(option))


(some pseudo code for select)

With this method you can realise a portscanner. It works correct but after 2 connection tests, WSAGetLastError return no WSAEWOULDBLOCK but WSAEINVAL ("The parameter s is a listening socket."). I dont know why, so I must close the socket and init a new socket for doing this *confused*

cu olli


*(Posted 2008) [#7]
thats about average for testing across the internet, my old system when testing with localhost only took a few seconds to tell me there wasnt a server present.


Winni(Posted 2008) [#8]
Thanks, Vertex, your module looks very interesting and very useful and I will take a good look at it (especially the Mac part). But although it works without C code, it's not based solely on original BlitzMax features but calls through to some OS APIs instead; this is nice and valid, but the original poster asked whether it can be done with pure BlitzMax functions, and then I think the answer is still 'no'.

Anyway - thanks for the hint to your module and for the work you put into it, this will come in very handy for some stuff I am going to write.