File transfer over TCP
Blitz3D Forums/Blitz3D Beginners Area/File transfer over TCP
| ||
Hi, im having real trouble understanding how the file/stream/bank commands work. I basically want to transfer a file in small chunks over the internet using TCP. I know FTP would be easier but i dont want to use it. Can anyone help with some simple code or at least an explanation of what needs to be done? Thanks |
| ||
Basically you have to open a stream - that is, a communication channel between two pcs, usually called the client and the server. Using this communication channel (the stream), you can send messages from a pc to another. The messages can be are strings, integers, or bytes; in other words, the data you want to send. So if you want to send a file using chunks, you have to use the same behaviour; each 'chunk' is like a message, and when you receive it, instead of print it to the screen, you fill in a file. A pseudo code would be: - (program that sends a file) connect to the server using stream send a string which represents the name of the file to be sent open the file_to_send for reading while file_to_send is not ended read a record from file write the record on the stream wend ;end of file write a value on the stream, that indicates that the file is ended close the communication with the server ;======================================== - (program that receives the file) open a server stream wait until someone connects accept the connection read the first message, which represents the name of the file to be written open the file for writing read message from the stream channel if the message is not the terminator file then write the message to the file else close the file endif ;---------------- end of pseudo code ----------------- In the code archive, there are interesting examples on various aspects about file transfer over internet. In my signature there's a link to the source code of a complete TCP chat program; perhaps can be useful to understand the grasp behind streams. Hope this helps, Sergio. |
| ||
cool, im gonna need that for the next part of my project. i quickly wrote something to see if i could copy a file and it works but i dont know why it works.. size=16384 bank=CreateBank(size) filein=ReadFile("C:\The reelists feat.Miss Dynamite - Back to life (One Love Album).mp3") fileout=WriteFile("C:\The reelists feat.Miss Dynamite - Back to life (One Love Album) copy.mp3") While Not Eof(filein) ReadBytes bank,filein,0,size WriteBytes bank,fileout,0,size Wend FreeBank bank CloseFile filein CloseFile fileout End The file gets copied and works just like the original. i want to know why this is working because it looks like im only reading bytes 0 to 16384 every loop.. Am i right in thinking that every time a chunk is read from a file, the pointer in the file automatically moves to the end of that chunk read? **** EDIT **** the above code is correct. |
| ||
Why do you expect that x should change ? It represents the offset where to start to read/write from/to. Am i right in thinking that every time a chunk is read from a file, the pointer in the file automatically moves to the end of that chunk read? I think no. You have to set the offset manually. |
| ||
no i dont expect x to change but im effectivly reading 16kb (0 - 16384 bytes) starting at offset 0 every loop.. i imagine this as reading the first 16kb of a file. if im always reading the first 16kb how am i managing to read the whole file which happens to be 4MB+ ? I just need to know HOW this is working. Sorry i dont mean to come accross rude, im just so frustrated with this. |
| ||
Hum... put a stop command in the while loop, set debug on, and inspect the x value at each loop. Perhaps it changes as you guess. If not, then maybe there's an internal pointer used, which is updated at each writebytes command. I have not blitz here, so I'm only guessing, since I did not use banks yet. Sergio. |
| ||
thanks ill do that.. can only think there is in fact a pointer that changes like you say. ill use the filepos command to check if it changes. |
| ||
just as i expected.. adding "print filepos(filein)" inside the loop shows that there is a pointer that auto updates when you readbytes OR writebytes. Strange how there is no mention of this in the docs.. oh well i can continue my project now. thanks for your help semar :) |