Reading file from internet..

Blitz3D Forums/Blitz3D Programming/Reading file from internet..

Chimeara(Posted 2008) [#1]
Is it possiable to open a file for reading form a website?
e.g.

ServerFile=ReadFile("www.darknimbuspro.com\RO.txt")


Zethrax(Posted 2008) [#2]
I couldn't get it to work using the file commands. I'd suggest that you use the TCP commands instead. Check out the docs for the 'OpenTCPStream' command for more info.

Note that the actual file contents will begin after the first blank line in the file is encountered. The text preceeding that is the tcp header.

eg.
connection = OpenTCPStream ( "www.darknimbuspro.com", 80)

If connection

	Print "Connected! Sending request..."
	
	WriteLine connection,"GET http://www.darknimbuspro.com/RO.txt HTTP/1.0" 
	WriteLine connection, Chr$( 10 )

	If Not Eof( connection )

		Print "Request sent. Waiting for reply..."
	
		While Not Eof( connection )
		
			Print ReadLine( connection )
			
		Wend

		Print "All data received."
		
	Else

		Print "ERROR: Failed to send request."
		
	EndIf

CloseTCPStream connection ; EDIT - Forgot to include this command.

Else

	Print "ERROR: Failed to connect to server."
	
EndIf

Print "Press a key to quit."

WaitKey

End



Chimeara(Posted 2008) [#3]
Thanks that's perfect


Chimeara(Posted 2008) [#4]
but is their a way to make sure no data is lost? is it possiable to use UDP for this... or only for LAN?


Zethrax(Posted 2008) [#5]
I believe the TCP protocol does extensive error checking to ensure that no data is lost. and that the data is recieved in the proper order.

UDP is a lower level standard which is not suitable for downloading files from a conventional web server. I think it does error checking for the data in a packet, but packets can be lost and are not necessarily recieved in the correct order. UDP is mainly designed for communicating between custom servers (gameservers, etc) and clients.

Note that if you wanted to download binary data (images, etc) from a website, your best bet is probably to use ReadLine until you encounter a blank line, and then use ReadBytes to read the binary data.


Chimeara(Posted 2008) [#6]
Yeah TCP is fine, it was just a bug in my code.

Next question:
How would you write to an internet file?

I've set the file permissions on the server to be writable... but i'm still not sure if it's possible to code in blitz?
My code so far is looking like this...

connection2 = OpenTCPStream ( "www.darknimbuspro.com", 80)
If connection2
Print "Connected! Sending request..."
WriteLine connection2,"GET http://www.darknimbuspro.com//RO.txt HTTP/1.0"
WriteLine connection2, Chr$( 10 )

Print "Request sent. Waiting for reply..."

WriteLine(connection2,ClientName)
WriteLine(connection2,ClientClass)
WriteLine(connection2,Clientlvl)
WriteLine(connection2,ClientUpdated)

Else
Print "ERROR: Failed to connect to server."
EndIf

Return
EndIf
EndIf EndIf

Wend


BlitzSupport(Posted 2008) [#7]
You can use the file commands on HTTP servers with the BlitzMax stream protocol http:: prefixed to the URL:

EDIT: DOH! BlitzMax code below, sorry!

ServerFile=ReadFile("http::www.darknimbuspro.com/RO.txt") 

If ServerFile = 0 Then End

Repeat

	Print ReadLine (ServerFile)
	
Until Eof (ServerFile)



Zethrax(Posted 2008) [#8]
To upload a file you would need to use file transfer protocol (FTP). I think there's some FTP code in the networking section of the code archives.

Note that I couldn't get BlitzSupport's code to work on Blitz3D. I assume he got confuddled and thought he was in the BlitzMax forums.


andy_mc(Posted 2008) [#9]
I've been thinking of using this sort of thing for games to automatically update themselves


Trader3564(Posted 2008) [#10]
Did you take a look at this example?
http://www.blitzbasic.com/codearcs/codearcs.php?code=2208

It works like this:
Local http:THTTP = New THTTP
'GET Example
If http.Open("www.fantasaar.com") 
	Print http.Get("version.php?client=game") 
else
	'error
End If
http.Close()



Zethrax(Posted 2008) [#11]
Remember that this is the Blitz3D programming forum. A lot of the code being entered in this topic is for BlitzMax.