BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TIdTCPServer/Client Sending raw data problem

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock
View previous topic :: View next topic  
Author Message
Matej
Guest





PostPosted: Fri Oct 24, 2003 8:28 am    Post subject: TIdTCPServer/Client Sending raw data problem Reply with quote



Hello! I did a lot of things with TClientSocket and TServer socket. That is
sending text, records, files in both way. Then I want to do the same thing
with Indy - TIdTCPServer and TIdTCPClient. And here I'm puzzled. I stuck
with sending raw binary files in direction Client to Server.

My idea was when client connects to server it sends a file and server saves
it in default folder.

This is the code:

Client side:

procedure TFormClientMain.Button1Click(Sender: TObject);
var
FStream: TFileStream;
begin
if OpenDialog1.Execute then
begin
FStream:= TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
with MyIdTCPClient1 do
begin
Connect;
WriteStream(FStream);
Disconnect;
end; { end of MyIdTCPClient1 }
FStream.Free;
end; { end of OpenDialog1.Execute }
end;



Server side:

On form create I activate the server

procedure TFormMyServerMainWindow.MyIdTCPServerExecute(AThread:
TIdPeerThread);
var
FStream: TFileStream;
begin
with AThread.Connection do
begin
memo1.Lines.Add('Connected');
FStream:= TFileStream.Create('C:tempppp', fmOpenWrite or fmCreate);
ReadStream(FStream,-1,true);
Disconnect;
FStream.Free;
memo1.Lines.Add('Disconnected');
end; { end of AThread.Connection }
end; { end of MyIdTCPServerExecute }

Port is the same like ip address. I'm using Delphi 6 enterprise (sp2) on
winxp.

What am I doing wrong? It nothing happens (actually it creates a file
tempppp but it is empty.
And it writes 'Connected' in memo1, but it does not write 'Disconnected').

I'm hoping that someone could help me...


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Fri Oct 24, 2003 6:20 pm    Post subject: Re: TIdTCPServer/Client Sending raw data problem Reply with quote




"Matej" <killer09 (AT) email (DOT) si> wrote

Quote:
I did a lot of things with TClientSocket and TServer socket. That
is sending text, records, files in both way. Then I want to do the
same thing with Indy

Everything TClient/ServerSocket can do, Indy can do, and usually better
(except for using non-blocking sockets, which Indy does not support - for
for just handling the data, it does everything).

Quote:
WriteStream(FStream);

You should do this instead:

WriteStream(FStream, True, True, 0);

That way, the stream sends its actual length as well, so the receiving end
can manage the data buffering easier.

You should also wrap the call in a try...finally block so that you can still
call Disconnect() correctly in case WriteStream() thrown an exception.

Quote:
memo1.Lines.Add('Connected');

You cannot do that from the OnExecute event. OnExecute is triggered in the
context of a worker thread, not the main VCL thread. You need to access the
Memo from the main VCL thread, though, in order to access it safely. You
need to use the worker thread's Synchronize() method to access components in
the main VCL thread safely.

Quote:
ReadStream(FStream,-1,true);

With the above change to WriteStream(), change this to the following
instead:

ReadStream(FStream,-1, False);

Or just:

ReadStream(FStream);

That way, it can read the length from the transmitted stream and preallocate
its internal buffers, instead of allocating/reallocating them during the
reading.

You should also wrap the call in a try...finally block so that you can still
free the file stream correctly in case ReadStream() thrown an exception.


Gambit



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.