File transfer over TCP

Blitz3D Forums/Blitz3D Beginners Area/File transfer over TCP

The Forum Archive is locked. These are the original blitzbasic.com forums, kept so their posts can still be read and searched. No new topics, replies or edits can be made here. To join the conversation, head over to the Blitz3D+ forums.

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.