Buffered file transfer with unlimited filesize
Posted: Thu Aug 10, 2006 11:22 pm
Code updated For 5.20+
Hi, i just managed to make a buffered file transfer test and it seems to work great. The file was sent successfully here. There is not too much error-checking inside but this can be optimized and improved by anyone. Don't forget to enter a valid file path in the client's code and after executing the server and the client to press a key in the client screen to begin the transfer. If you manage to make it better please share it because this is a pretty good improvement to the network functions. We can add a callback too
.
The server.pb file
The client.pb file (execute after the server and press a key in the console)
Hi, i just managed to make a buffered file transfer test and it seems to work great. The file was sent successfully here. There is not too much error-checking inside but this can be optimized and improved by anyone. Don't forget to enter a valid file path in the client's code and after executing the server and the client to press a key in the client screen to begin the transfer. If you manage to make it better please share it because this is a pretty good improvement to the network functions. We can add a callback too

The server.pb file
Code: Select all
;by Inf0Byt3
OpenConsole()
#Command = "!ASLKLK#@*)SAUJ!"
#DataBuf = "!SI#)()U!J:LSAJ!"
Global FileHandle
*Buffer = AllocateMemory(1024)
If InitNetwork()
CreateNetworkServer(0, 6443, #PB_Network_TCP)
PrintN("Server created...")
Repeat
SEvent = NetworkServerEvent()
If SEvent
ClientID = EventClient()
Select SEvent
Case 1
Case 2
ReceiveNetworkData(ClientID, *Buffer, 1024)
Command.s = PeekS(*Buffer,128)
;00000000000000000000000000000
Select Left(Command,16)
Case #Command
DataRecv.s = Mid(Command,17,Len(Command)-16)
Filesize.l = Val(StringField(DataRecv,1,"|"))
Filename.s = StringField(DataRecv,2,"|")
FileHandle = CreateFile(#PB_Any,"Received.txt")
Case #DataBuf
LengthRec = Val(RemoveString(Mid(Command,17,8),"X"))
WriteData(FileHandle,*Buffer+24,LengthRec)
EndSelect
FreeMemory(*Buffer)
;00000000000000000000000000000
Case 4
Quit = 1
EndSelect
EndIf
Until Quit = 1
CloseNetworkServer(0)
Else
MessageRequester("Error", "Can't create the server (port in use ?).", 0)
EndIf
Code: Select all
OpenConsole()
#Command = "!ASLKLK#@*)SAUJ!"
#DataBuf = "!SI#)()U!J:LSAJ!"
If InitNetwork() = 0
PrintN("Can't initialize the network !")
Input()
End
EndIf
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
File.s = "yourfilepathhere"
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Global Sent,Length
ConnectionID = OpenNetworkConnection("127.0.0.1", 6443, #PB_Network_TCP)
If ConnectionID
;Repeat
SendNetworkString(ConnectionID, #Command + Str(FileSize(File))+"|"+GetFilePart(File))
Input()
If ReadFile(0,File)
Length = Lof(0)
While Sent < Length
*DataSend = AllocateMemory(1024)
*Temp = AllocateMemory(1000)
DataR = ReadData(0,*Temp,1000)
BufferLength.s = LSet(Str(DataR),8,"X")
Signature.s = #DataBuf+BufferLength+"!"
PokeS(*DataSend,Signature)
CopyMemory(*Temp,*DataSend+24,DataR)
SendNetworkData(ConnectionID,*DataSend,DataR+24)
FreeMemory(*Temp)
FreeMemory(*DataSend)
Sent + DataR
PrintN(Str(Sent)+" -> "+Str(Length)+" -> "+Str(x))
x + 1
Wend
CloseFile(0)
CloseNetworkConnection(ConnectionID)
EndIf
Input()
Else
MessageRequester("Client", "Can't find the server!!! Is it launched ?", 0)
EndIf