 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matej Guest
|
Posted: Fri Oct 24, 2003 8:28 am Post subject: TIdTCPServer/Client Sending raw data problem |
|
|
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
|
Posted: Fri Oct 24, 2003 6:20 pm Post subject: Re: TIdTCPServer/Client Sending raw data problem |
|
|
"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 |
|
 |
|
|
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
|
|