Sending Files

BlitzPlus Forums/BlitzPlus Programming/Sending Files

how can i send a file through a UDP/TCP stream?

I provide this code as is, sorry for being so lame.

These are sent through TCP, the only way that I would send data for the most-guaranteed highest success rate.

The sender should run the file send code, the receiver should run the file receive code. Downloads go into a "downloads" folder which will automatically get made.
The IP of the receiver must be known and the receiver must not be firewalled.

If anyone has any questions, please speak up.

I made this just for myself so that I won't always use AIM with friends.

There is a send limit, the number of bytes of the file must fit into the integer type, so dont try multi-gig files. Everything else should work ;)

FileSend


fileName$ = RequestFile("File to Send","",False)


chunksize = 10*1000
completed = 0

;Print myString$
;
;WaitKey()

If fileName$ = "" Then End

fileIn = ReadFile(fileName$)

If fileIn = 0 Then
	Notify "File Cannot Open",True
	End
Else
	
	
	
	
	cw=ClientWidth(Desktop())
	ch=ClientHeight(Desktop())
	windwid=200
	windhei=50
	ipWindow = CreateWindow("Dotted IP: ",cw/2-windwid/2,ch/2-windhei/2,windwid,windhei,0,1+2+16)
	ipWindowTextField = CreateTextField(0,0,windwid,windhei-5,ipWindow)
	SetMinWindowSize ipWindow
	
	SetGadgetText ipWindowTextField,"127.0.0.1"
	
	
	ActivateGadget ipWindow
	ActivateGadget ipWindowTextField
	
	Repeat
		Select WaitEvent()
		Case $803
			End
		Case $401
			If EventSource() = ipWindowTextField And EventData() = "13" Then
				ip$ = TextFieldText$(ipWindowTextField)
				FreeGadget ipWindow
				Exit
			EndIf
		End Select
	Forever
	
	
	TCPStream = OpenTCPStream(ip$,5001)
	
	If TCPStream<>0 Then
		Print "pass"
	Else
		Print "fail"
		Delay 5000
		End
	EndIf
	
	
	fileNameToSend$ = fileName$
	char = Len(fileName)
	While Mid$(fileName$,char,1)<>"" And char>=0
		fileNameToSend$ = Right$(fileName$,Len(fileName)-char+1)
		char=char-1
	Wend
	
	WriteString TCPStream, fileNameToSend$
	WriteInt TCPStream, FileSize(fileName$)
	WriteInt TCPStream, chunksize
	
	theFile = ReadFile(fileName$)
	
	
	
While completed<FileSize(fileName$)

If completed+chunksize>FileSize(fileName$) Then chunksize = FileSize(fileName$)-completed

	bank = CreateBank(chunksize)
	
	ReadBytes(bank,theFile,0,chunksize)
	WriteBytes(bank,TCPStream,0,chunksize)
	Print (Int(10000.0*completed/FileSize(fileName$))/100.0)+" %"
	
	completed=completed+chunksize
	
	FreeBank bank
	
Wend
	
	
	
	CloseFile theFile
	
	Notify "File sent",False
	
EndIf


File Receive


testFolder = ReadFile("downloads")

If ReadFile <> 0 Then
	If FileType(testFolder) = 2 Then
		Goto itsok
	Else
		Notify "Cannot download to folder 'downloads'",True
		End
	EndIf
Else
	CreateDir "downloads"
EndIf

.itsok



TCPserver = CreateTCPServer(5001)

If TCPserver Then
	Print "initialized"
Else
	Print "failed"
	End
EndIf



Repeat
WaitEvent(100)
incoming = AcceptTCPStream(TCPserver)
If incoming Then
	fileName$ = ReadString(incoming)
	
	theFile = WriteFile("downloads"+fileName$)
	If theFile = 0 Then
		Print "file cannot save"
		Delay 5000
		End
	Else
		Print "starting transmission"
	EndIf
	
	sizeOfFile = ReadInt(incoming)
	
	chunksize = ReadInt(incoming)
	completed = 0
	

While completed<sizeOfFile
WaitEvent(1000)
	If completed+chunksize>sizeOfFile Then chunksize=sizeOfFile-completed
	
	bank = CreateBank(chunksize)
	
	c=ReadBytes(bank,incoming,0,chunksize)
	If c>0 Then
		Print (Int(10000.0*completed/sizeOfFile)/100.0)+" %"
	EndIf
	
	WriteBytes(bank,theFile,0,chunksize)
	
	completed=completed+chunksize
	
	FreeBank bank
	
Wend
	
	CloseFile theFile
	
	Print "transmission successful"
	Exit
EndIf

Forever


WaitKey()