Net Connection Detection and File Download?
BlitzMax Forums/BlitzMax Programming/Net Connection Detection and File Download?
| ||
1) Is there a way to detect if a net connection is available without actually trying to use it? IE: I want to know if an internet connection is available without pinging a site or anything else which would trigger a firewall. If there is no connection available, I don't want to do a thing. 2) How would I go about downloading a file from a website via http in BMax? Opening a stream and reading bytes seems too simple, and probably doesn't account for lag, site failures, a lost connection, etc. I don't want to corrupt the game every time something goes wrong, so what exactly must I and can I do to make this "correct"? |
| ||
Not strictly BMAX since we have built these functions into our own dll. 1) Polling the connection will automatically open it even if you are not pinging a host. If there is no network connection at all (no modem or NIC installed then the dll function will return False). Initialising the dll will either open the connection (return True) or return False. 2) Downloading a complete file could be problematic for the reasons you mentioned, you would either need to stall the program execution whilst you attempted to pull the complete file or (and I am assuming you are talking downloading question files) read them from a database using an SQL query and read the responses into a buffer. When the server response is complete transfer them into the game. The latter can be done in the background and the player could continue to play the game whilst you are checking for and downloading new questions. We do exactly that in the dll for downloading highscores - in reality you could be downloading any information just by altering the script. We are not due to release yet but drop me a line if you wanna pre-release test it, I could get you something in a couple of days :D |
| ||
Tim, I'm up to pre-release test it if it's ok. |
| ||
Tim, Thanks for the offer. I've been looking through the PB forums and it seems there is some API stuff to detect a connection without opening it, so I may give that a go. Questions are one of the reasons I posted, but I don't really want to go down the SQL road. I would like to use it for other things as well, and they won't always be suitable to be a db. I could live with FTP. I see AlienCodec has a patching program out, but it looks like a slightly clunky approach, and with the number of people posting that their support requests are being ignored, I reckon I'll give that a miss. I've looked around for any file transfer libraries too, but the only one I found so far is an ActiveX DLL, so no good for BMax. Perhaps I'll look into writing a PB DLL to do it for me. I guess PB supports threads, so that would be ok. |
| ||
There is a free http/ftp all in one library out there but I cant remember the name for the life of me! But I did find this - which might be some help. http://www.codeproject.com/internet/FTPClientClass.asp |
| ||
I have written an FTP library for BlitzMAX if you want that. If you want to verify the data, find some code to compute a hash for it, download that hash first, then download the data. Recompute the hash for the data actually downloaded and then compare it against the original hash. If they match then you can use the file, else it is corrupted. I suggest doing some real-world tests to determine whether corruption is a problem first before you go down this route. It may be the case that the HTTP stream already has enough built-in mechanisms to deal with transmission errors. |
| ||
I've heard that the Win32 API function InternetGetConnectedState () isn't always reliable, but I don't think anything is, oddly enough. Anyway, if the Online () function below returns True, you're theoretically online... [EDIT: Oops, Win32 only... not progressed to other OSes' API calls yet!] ' Function pointer... Global Online (flagsptr:Int Ptr, reserved) ' Win32 constants... Const INTERNET_CONNECTION_CONFIGURED = $40 Const INTERNET_CONNECTION_LAN = $02 Const INTERNET_CONNECTION_MODEM = $01 Const INTERNET_CONNECTION_MODEM_BUSY = $08 Const INTERNET_CONNECTION_OFFLINE = $20 Const INTERNET_CONNECTION_PROXY = $04 Const INTERNET_RAS_INSTALLED = $10 ' Load wininet.dll and assign function pointer... wnet = LoadLibraryA ("wininet.dll") If wnet Online = GetProcAddress (wnet, "InternetGetConnectedState") EndIf ' Flags integer receives details of connection... Local flags = 0 ' Call function... result = Online (Varptr (flags), 0) ' Show results... If result Print "" Print "Connection found!" Print "" If flags & INTERNET_CONNECTION_CONFIGURED Print "Valid connection found" EndIf If flags & INTERNET_CONNECTION_LAN Print "LAN-based connection to internet" EndIf If flags & INTERNET_CONNECTION_MODEM Print "Modem-based connection to internet" EndIf If flags & INTERNET_CONNECTION_MODEM_BUSY Print "No longer used" EndIf If flags & INTERNET_CONNECTION_OFFLINE Print "Currently offline" EndIf If flags & INTERNET_CONNECTION_PROXY Print "Proxy connection to internet" EndIf If flags & INTERNET_RAS_INSTALLED Print "RAS installed" EndIf Else Print "Not connected!" EndIf |