Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TIdTCPServer/Client Sending raw data problem

Status
Not open for further replies.

killer09

Programmer
Oct 24, 2003
1
SI
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...
 
I've just been experimenting with using Compression with the Indy TCP Server and client, and so I know my code works:

It looks pretty much exactly like yours, except I only specified fmCreate with creating the tfilestream. Try that. And, because the 'Disconnect' is not being written to your Memo component, you know that an exception is occurring somewhere in there. You're not seeing it because the exception is occuring within a Thread (The TidTCPServer is multi-threaded), and Threads do not pass exceptions out to the main VCL thread - you have to handle *all* exceptions within the thread.

So, modifying your code to this:

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

Will output any exception messages to your Memo, and hopefully explain further what's going on. Post back if you need more help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top