Large files over NET [SOLVED]
Posted: Sat Jan 22, 2011 5:41 pm
I want to modify the code to send/receive list of files so the server side does not close connection and continue listen PORT
Here is the code
Here is the code
Code: Select all
;Buffered file transfer with unlimited filesize
;Server
;by Inf0Byt3
; OpenConsole()
#Command = "!ASLKLK#@*)SAUJ!"
#DataBuf = "!SI#)()U!J:LSAJ!"
Global FileHandle
*Buffer = AllocateMemory(1024)
If InitNetwork()
CreateNetworkServer(0, 6443, #PB_Network_TCP)
Debug("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,"|")
SavePath.s = StringField(DataRecv,3,"|")
FileHandle = CreateFile(#PB_Any,SavePath.s)
Case #DataBuf
LengthRec = Val(RemoveString(Mid(Command,17,,"X"))
WriteData(FileHandle,*Buffer+24,LengthRec)
EndSelect
FreeMemory(*Buffer)
;00000000000000000000000000000
Case 4
Quit = 1
EndSelect
EndIf
Until Quit = 1
CloseFile(FileHandle)
CloseNetworkServer(0)
Else
MessageRequester("Error", "Can't create the server (port in use ?).", 0)
EndIf
Клиент
;Client
OpenConsole()
#Command = "!ASLKLK#@*)SAUJ!"
#DataBuf = "!SI#)()U!J:LSAJ!"
If InitNetwork() = 0
PrintN("Can't initialize the network !")
Input()
End
EndIf
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
File.s = "Source"
SavePath.s="Destination"
;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Global Sent,Length
ConnectionID = OpenNetworkConnection("127.0.0.1", 6443, #PB_Network_TCP)
If ConnectionID
;Repeat
SendNetworkString(ConnectionID, #Command + Str(FileSize(File))+"|"+GetFilePart(File)+"|"+SavePath.s+"|")
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