Dark Bit Factory & Gravity

PROGRAMMING => Other languages => Blitz => Topic started by: Paul on January 05, 2007

Title: send file?[BB2D]
Post by: Paul on January 05, 2007
Hello!

Is it possible for blitz to send a file from 1 computer to an other?
If so how?

i'm thinking encoding the file with a 64 bit encoder, sending it and then decoding it but that would be verry inefficient.

 
Title: Re: send file?
Post by: Stonemonkey on January 05, 2007
Yep, blitz can send data using TCP or UDP. For sending a file use TCP, look up TCP(network) in the command docs as there should be some small examples there.
Title: Re: send file?
Post by: Paul on January 05, 2007
isnt that just gor sending text?
i mean for sending an image or an exe file
Title: Re: send file?
Post by: Stonemonkey on January 05, 2007
It can be used to send data of any type, not only strings. Look up readint/writeint, also for bytes or floats.
Title: Re: send file?
Post by: Clyde on January 05, 2007
I think that Blitz Plus has much more features for using web based stuff.
Title: Re: send file?
Post by: Stonemonkey on January 05, 2007
The basic networking stuff on both are the same afaik but is there anything else in blitzplus that would be of use here?
Title: Re: send file?
Post by: Paul on January 05, 2007
If i forexample want to send an exe file to an other computer how would i do???
Title: Re: send file?
Post by: Stonemonkey on January 05, 2007
You'll need to open a TCP stream between the 2 machines, can't really remember how to do that atm but once they're connected:

the source machine sends the name (and type) of the file you're wanting to send then opens the file and reads and sends all the data from that file

while the destination machine recieves that data and saves it to disk using the filename/type it recieved.

While you're working on this, instead of trying to get it working using 2 machines you can use the loopback ip address 127.0.0.1 and have the client/server both running on 1 machine.
Title: Re: send file?
Post by: Paul on January 05, 2007
just one thing.
do i use readline, readstring or read what??
Title: Re: send file?
Post by: Stonemonkey on January 05, 2007
I would probably use write/readstring to send the filename and then send the file data as bytes although i think i've read somehwere that it's faster to put bytes into a string and send that but tbh it's been a while since i've done anything like this.
Title: Re: send file?
Post by: Paul on January 05, 2007
Its working :)

Code: [Select]
; 1 st code set
server=CreateTCPServer(8080)
If server=0 Then Print ":(" WaitKey():End
Print "server started"
Print "waiting"
While Not stream
stream=AcceptTCPStream(server)
Wend
Print "connected"
file=WriteFile(ReadString$(stream))
Print "reciving file"
While Not KeyHit(1)

If Not Eof(stream)
WriteByte(file,ReadByte%(stream))
Else
Print "done"
CloseFile(file)
WaitKey()
End
EndIf

Wend
CloseFile(file)
End

Code: [Select]
;2nd code
server=OpenTCPStream("192.168.0.2",8080)

If server=0 Then Print ":(" WaitKey():End
file=OpenFile("C:\Documents and Settings\david1.DAVID\Desktop\paul\meny.bmp");<-- change this to your image( or ;other file
WriteString(server,"meny.bmp")<--And this
Print "sending"
While Not KeyHit(1)
If Not Eof(file)
WriteByte(server,ReadByte%(file))
Else
Print "done"
EndIf
Wend

Quote
i've read somehwere that it's faster to put bytes into a string

how long can a string be?

Thx for the help Stonemonkey :D .)
Title: Re: send file?
Post by: Stonemonkey on January 05, 2007
Good work paul, I'm not sure about the string length but i'd put some sort of limit on it.
Title: Re: send file?
Post by: Paul on January 06, 2007
Just want to post the finished code :) or well a working code. i've done a function that doesn't return until the file is sent but it will show ny1 how th do. hope someone find it useful

Code: [Select]
Select recivefile()
Case  1: Print "Sucess"
Case  0: Print "unknown???"
Case -1: Print "Time out"
Case -2: Print "unable to write file"
Case -3: Print "file my be dead"
End Select
WaitKey()

Edit:
forgot to say there is  bug somewhere, the file gets a little bit bigger for some reason...

Code: [Select]
Function recivefile()
server=CreateTCPServer(8080)
If server=0 Then Return 0
;Print "server started"
;Print "waiting"
While Not stream
tmp=tmp+1
stream=AcceptTCPStream(server)
Delay 100
If tmp>600 Then Return -1
Wend
;Print "connected"
file=WriteFile(ReadString$(stream))
If file=0 Then Return -2
;Print "reciving file"
Print "Recieving Mr.file"
Repeat
If Not Eof(stream)
tmpo$=ReadString$(stream)
For i=0 To 1024
tmp2$=Mid$(tmpo$,(i+1)*3,3)
; Print tmp2$:WaitKey():End
If tmp2$="sto" Then Exit
WriteByte(file,tmp2$)
Next
wistro=wistro+1
Print wistro
Else
If file<>0
CloseFile(file)
Return 1
EndIf
Return -3
EndIf
Forever
End Function

;***********************************************
;***********************************************
;***********************************************
;***********************************************
;***********************************************
;***********************************************

Select sendfile("C:\Documents And Settings\david1.DAVID\Desktop\paul\","MUSIC9.MID","hubba.mid","127.0.0.1");"192.168.0.2")
Case  1: Print "sucess"
Case  0: Print "unknown???"
Case -1: Print "Time out"
Case -2: Print "unable to read file"
Case -3: Print "unknown???"
End Select
WaitKey()

Function sendfile(path$,name$,dname$,ip$)
Repeat
server=OpenTCPStream(ip$,8080);"192.168.0.2"
Delay 100
a=a+1
Print a
Forever
If server = 0 Then Return -1
file=OpenFile(path$+name$)
If file   = 0 Then Return -2

WriteString(server,dname$)
Print "sending"

Repeat
If Not Eof(file)
tmp$="aa"
For i=0 To 1024
tmp2$=ReadByte%(file)

If Eof(file) tmp$=tmp$+"sto":Exit
;Print tmp$+String("0",(3-Len(tmp2$)))+tmp2$
tmp$=tmp$+String("0",(3-Len(tmp2$)))+tmp2$
If i=1 Then tmp3$=tmp$
Next

tmpas=tmpas+1
Print tmpas
WriteString(server,tmp$)
;Print tmp3$ WaitKey():End
;Delay 100
Else
If server<>0
CloseTCPStream(server)
Return 1
Else
Return -3
EndIf
EndIf
Forever
End Function
Title: Re: send file?
Post by: mike_g on January 06, 2007
Cool, I havent worked with any of the Blitz networking commands yet so I'll check this out and try learn something new :)
Title: Re: send file?
Post by: Paul on January 09, 2007
Hi again

There is a problem with the above code, It doesn't work online, only lan. atleast for me. if anyone finds the problem (or a fix) please tell me. Or if it works for someone else .
Title: Re: send file?
Post by: Stonemonkey on January 09, 2007
Ah, I had the same problem when i replaced my modem with a router and I don't think I found a solution but there must be something that can be done.
Title: Re: send file?
Post by: Jim on January 09, 2007
Quote
There is a problem with the above code, It doesn't work online, only lan. atleast for me. if anyone finds the problem (or a fix) please tell me. Or if it works for someone else .
It won't work if both ends are using NAT (Network Address Translation), the same way that BitTorrent or eDonkey goes into passive mode and you can't hardly see anyone.  One end needs to un-NAT (turn the NAT router into a bridge, perhaps you have menu option on the router) or both ends need to use uPNP to set up a connection.  Whether uPNP's possible from blitz I wouldn't know, probably not.  Be careful turning off NAT though, it leaves your computer wide open to the net unless you have a good software firewall (NOT Windows firewall as it doesn't block incoming connections).

Something else to try.  Enable port forwarding on your router for the port number you are using (should be > 49151) so when your friend tries to send you a file, it gets directed to the correct computer on your LAN.  This is how you enable BitTorrent etc to work properly.

Jim
Title: Re: send file?
Post by: Paul on January 11, 2007
So in simpler terms you're saying that my friends router firewall is blocking me?
Title: Re: send file?
Post by: Stonemonkey on January 11, 2007
It's more to do with how the router knows which address on the LAN that the data is meant to be going to, the IP that you try to connect to will just be the address of the router on the internet and setting up port forwarding for a particular port number will tell the router which address on the LAN that the data coming in on that port is meant for.
Title: Re: send file?
Post by: Jim on January 11, 2007
Quote
So in simpler terms you're saying that my friends router firewall is blocking me?

The problem is NAT.

All traffic from a LAN which is connected to the net by a NAT router appears to the outside world to come from the router.  When someone tries to talk to you from outside they only talk to the router.  This means if the router has not been told where to direct that traffic it will be dropped.  The way to fix this is port forwarding.  When someone connects to your router on a given port number (you chose 8080) then you need to tell your router (using its configuration panel) to forward any traffic on that port to a specific IP address on your LAN.

As you can see, NAT is also a pretty good security feature.  Your router drops all unforwarded inbound traffic as if your LAN didn't exist.

Btw, you shouldn't use port 8080, it's registered already as being used for HTTP traffic.  If your message goes via a proxy server (anywhere along the way) it might get corrupted or dropped unless it actually looks like http.
You should use a port above 49151.
Port numbers can be found here http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers (http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers)

Jim