jump to navigation

Data Transfer using TSocket in Delphi Desember 26, 2006

Posted by Tyo in Delphi.
trackback

Recently i got a project that from my perspective is very challenging, and that is create an application that consist of two app, server and client.The client should receive a list of directory from the server app that is in the remote computer, so that computer where the client app resides could select the file or directory for download (much like RAdmin). The challenging part is that the project must not use a Delphi component that based on FTP and HTTP model. so there is no other option that i have to use TSocket.

after all day researching, and trying to compile the app. I found out, that for the first time i have to create a protocol. where in this protocol, i have to define what the client app should send to server app and what the respond from the server app. it will looks like this :

server part :

if Pos(‘LIST!’,strCommand) = 1 then

begin

if DirectoryExists(strFile) then

begin

strFeedback := ‘LIST!’;

Socket.SendText(strFeedBack);

AllDir; //Create directory List

end

else

begin

strFeedBack := ‘ERROR’ + strFile + ‘ Directory is not Found or a File’;

socket.SendText(strFeedback);

end;

end;

for the client Part : (this is a part of case .. of statement)

csList :

begin

listFiles.Items.Text := Socket.ReceiveText;

cliStatus := csIdle;

end;

I found this statement from Mastering Delphi 4 by Marco Cantu.

 

i find some funny thing in the client app when it’s receiving the transfer data :

when i put this code :

begin
Stream2 := TFileStream.Create(SaveFileName,fmOpenWrite or fmCreate );
Screen.Cursor:=crHourGlass;
try
while true do
begin
nReceived := Socket.ReceiveBuf(Buffer,sizeof(Buffer));
if nReceived

end;

time1 := GetTickCount;

while GetTickCount – time1
stream.Position := 0;
end;

this tranfer and receive process only receiving 16 Kb data, with all of them corrupt.

but if i change the code with some extra variable that using TMemorystream, then i can transfer data in any kind format with size until 200 Mb. it’s amaze me, but it still seems wierd to me, since i have to use 2 Stream variable.

the code will be looks like this :

begin

Stream :=TMemoryStream.Create;

Stream2 := TFileStream.Create(SaveFileName,fmOpenWrite or fmCreate );

Screen.Cursor:=crHourGlass;

try

while true do

begin

nReceived := Socket.ReceiveBuf(Buffer,sizeof(Buffer));

if nReceived

else

begin

Stream.Write(Buffer,nReceived);

Stream2.Write(Buffer,nReceived);

end;

time1 := GetTickCount;

while GetTickCount – time1

stream.Position := 0;

end;

the application can work perfectly, and the data that is being transferred can work to.

Komentar»

1. Pietras - April 26, 2007

Good article, but what are vars nreceived and buffer? write type of all vars.

answer :
it just for counting the filesize that should be send to other computer. thanks

hope it helps.

thank you