This is very raw and cant handle multiple streams or can the server be used more than once without shutting down, this is just a test if i could send a file from china to mexico.
Code archives/Networking/TCP - Sending a File
This code has been declared by its author to be Public Domain code.
Download source code
| This send file data from one computer to another, Server asks for a filename (ex: File.txt) then it measures the file size of the file. When a client connects to the ip address, the server sends the client 2 things, the filename of the file thats about to be sent, and the file size. The client calculates the current byte count while the file is being downloaded and sees if the real file size and the current size are both the same. |
;Server Code Graphics 640,480,16,2 AppTitle "Server Example" filetosend$=Input("File:") file1=OpenFile(filetosend$) FileSize1=FileSize(filetosend$) Print "File Size:"+filetosend$+" "+FileSize1 server=CreateTCPServer(7000) a=1 Repeat stream=AcceptTCPStream(server) If stream Then If a=1 Then WriteLine(stream, filetosend$) WriteLine(stream, filesize1) a=0 EndIf While Not Eof (file1) Data1=ReadByte(file1) WriteLine(stream, Data1) Wend EndIf Forever ;End of Server Code ;Client Code AppTitle "Client Example" ip$=Input("IP:") port=Input("PORT:") strmGame=OpenTCPStream(ip$,port) If strmGame<>0 Then Print "Client Connected successfully." Else Print "Server failed to connect." WaitKey End End If filetoget$=ReadLine(strmgame) file=WriteFile("1"+filetoget$) filesize2#=ReadLine(strmgame) Print "Downloading 1"+filetoget$+" from "+ip$+"("+port+")" Print "Size: " + filesize2# + " Bytes" Repeat Data1=ReadLine( strmGame ) a#=a#+1 printpercent#=a#/filesize2# printpercent#=printpercent#*100 AppTitle printpercent#+"%" WriteByte(file, data1) Until a# = filesize2# ;End of Client Code |
Comments
To make it so that you can use it more than once I think Types could be used? Not sure, though, how it would work.
now that is really something to learn from :)
it is very short and since it can send anything else besides a file, i will take a look and learn from the professionals.
it is very short and since it can send anything else besides a file, i will take a look and learn from the professionals.
hm, since one certain moment it stopped working and AcceptTCPStream(...) always returned 0. even after rebooting windows xp it didnt help.
am i doing anything wrong??
am i doing anything wrong??
I tested it out, using B3D and it works properly. If you are doing it locally make sure your using 127.0.0.1 and port 7000