Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Anyone know of a demo to show a complete idiot like me how to do...

1 view
Skip to first unread message

Gary Williams

unread,
Feb 7, 2002, 2:05:06 PM2/7/02
to

"Martin Dew" <mg...@btinternet.com> wrote in message
news:3c62cd48_2@dnews...
> I want to use an Indy TCP/Ip client and TCPIP server component and safely
> transmit a file from the client to the server then disconnect.


I had to figure this out myself not too long ago.

In the following code, just fill in the blanks, and put a response message
in Memo2.

Client
------

var
PostData: TStream;
Response: TStringStream;

...

PostData := T___Stream.Create();
Response := TStringStream.Create('');
IdHTTP := TIdHTTP.Create(nil);
try
IdHTTP.ReadTimeOut := ___ * 1000;

try
IdHTTP.Request.ContentType := ___;
IdHTTP.Post(URL + ':' + Port, PostData, Response);

ShowMessage(Response.DataString);
except
on E: Exception do
ShowMessage(E.Message);
end;

finally
IdHTTP.Free;
PostData.Free;
Response.Free;
end;

Server
------

procedure TForm1.FormCreate(Sender: TObject);

begin
IdHTTPServer1.DefaultPort := ____;
IdHTTPServer1.Active := True;
end;

procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread;
RequestInfo: TIdHTTPRequestInfo;
ResponseInfo: TIdHTTPResponseInfo);

begin
Memo1.Text := RequestInfo.UnparsedParams;
ResponseInfo.ContentText := Memo2.Text;
RequestInfo.RawHeaders.ConvertToStdValues(Memo3.Lines);
end;

-Gary

Martin Dew

unread,
Feb 7, 2002, 1:47:46 PM2/7/02
to
I want to use an Indy TCP/Ip client and TCPIP server component and safely
transmit a file from the client to the server then disconnect.

I am completely new to anything winsock related and would really appreciate
either advice or a step by step guide on what I need to do. I have looked at
the indy demo's that ship with Delphi and just purely get confused.

Thanx in advance to anyone that can help me.

Martin


Martin Dew

unread,
Feb 7, 2002, 2:02:10 PM2/7/02
to
thanx gary.

is there any particular reason why you went for http stuff instead of using
writefile on a tcpclient ?

Martin


Gary Williams

unread,
Feb 7, 2002, 3:05:51 PM2/7/02
to

"Martin Dew" <mg...@btinternet.com> wrote in message
news:3c62d0a8_1@dnews...

I based this code off of something I did previously, where HTTP was required
per the written specification. Perhaps it can easily be modified to better
suit your liking.

-Gary


arthurox

unread,
Feb 8, 2002, 8:31:28 AM2/8/02
to
"Martin Dew" <mg...@btinternet.com> wrote in message
news:3c62d0a8_1@dnews...


Hmm, he's not showing how it's done at all either.. He's just showing you
his own adaptation of the HTTP Server demo.
It works fine tho..

A better example is to look at the TCPStreamClientServer demo that is in the
demo's section on the Indy FTP site.

Sending files is done using the ReadStream/WriteStream functionality of
TCPServer/TCPClient.
The HTTPServer is just a derivative of the TCPServer, and thus effectively
uses the same functionality.

Always keep in mind that if you send streams you MUST have a way of telling
the other side on how many bytes it should expect.
In 'pseudo' code it's something like this:

Client -> Server : I want File 'MyFunnyBird.JPG'.
Server -> Client : WriteLn MyFunnyBird.JPG is xxxx bytes in size.
Client -> : ReadStream(TFileStream,xxxx);
Server -> : WriteStream(TFileStream,xxxx);
Client -> Server : I received xxxx bytes.
Server -> Client : Done.

Perhaps for some reason you might want to use a TMemoryStream to load the
file into, and then send it, but a TFileStream works just as well and saves
you a few kbytes of memory at the expense of a few cpu cycles..

Indy's Read/Write Stream functions can handle any of these streamtypes, but
essentially above pseudocode could be implemented with any tcp/ip component
suite, although i'd stay away form NetMa$ter$ suite, it'll probably give you
a headache..

Hth,

Marco.


Martin Dew

unread,
Feb 11, 2002, 1:32:18 PM2/11/02
to
hey thanx. What is the address for the ftp site ?? I cannot see alink to
what you are telling me to check out on the indy website ?

Don Siders [Team Indy]

unread,
Feb 11, 2002, 8:34:58 PM2/11/02
to

"Martin Dew" <mg...@btinternet.com> wrote in message
news:3c680fad_1@dnews...

> hey thanx. What is the address for the ftp site ?? I cannot see alink to
> what you are telling me to check out on the indy website ?

Demos and FTP access are linked on the Download page at
http://www.nevrona.com/Indy/download90.html. They're near the bottom of the
page.

hth...
--
Don


Posted by ELKNews 1.0.4-B
Empower your News Reader! http://www.atozedsoftware.com

Peter I. Dunne (Team GDA)

unread,
Feb 21, 2002, 5:31:16 AM2/21/02
to
Hi all,
Just to throw a spanner in the works
You failed to mention that the server is multithreaded and therefore the
filestream should be created in the onConnect event and assigned to the data
object of the thread
if it is a global object then having 2 or more clients connected can really
mess up your day

what I use is a bit like this but uses a totally different data object (not
fully tested yet)
I am also new to indy so please correct me if I am wrong on anything

procedure TFormTCPServer.TCPServerConnect(AThread: TIdPeerThread);
begin
try
AThread.Data:=TFileStream.Create;
except
AThread.Data:=nil;
end;
end;

procedure TFormTCPServer.TCPServerDisconnect(AThread: TIdPeerThread);
begin
if Assigned(AThread.Data) then
Try
Tobject(Data).Free;
AThread.Data:=nil;
except
StatusBar.SimpleText:='Error Destroying Server DataModule';
end;
end;

procedure TFormTCPServer.TCPServerExecute(AThread: TIdPeerThread);
var Data:TFileStream;
begin
Data:=pointer(AThread.Data);
// we put our file handling code here
End;

--
Regards
Peter I. Dunne

http://www.gda.bizhosting.com (A developers site by developers for
developers)
No attachments in replies unless requested please

Don Siders [Team Indy] wrote in message <5BB6AF715B3...@att.net>...

Arthuro

unread,
Feb 25, 2002, 5:27:00 AM2/25/02
to
"Peter I. Dunne (Team GDA)" <refuge...@hotmail.com> wrote in message
news:3c74cc76$1_2@dnews...

> Hi all,
> Just to throw a spanner in the works
> You failed to mention that the server is multithreaded and therefore the
> filestream should be created in the onConnect event and assigned to the
data
> object of the thread
> if it is a global object then having 2 or more clients connected can
really
> mess up your day
>
> what I use is a bit like this but uses a totally different data object
(not
> fully tested yet)
> I am also new to indy so please correct me if I am wrong on anything

Well this code looks fine. But there's no obligation to create the
filestream in the onConnect event.
You can create it at any moment anywhere, because you always have access to
the data object.

You could set up a little protocol that says something like this:

Get Ready -> This is the time to create your filestream
SendStream
StreamDone -> Now you can free the filestream..


[snip]


Johannes Berg

unread,
Feb 26, 2002, 12:49:12 PM2/26/02
to
> Get Ready -> This is the time to create your filestream
> SendStream
> StreamDone -> Now you can free the filestream..

But don't rely on clients to send it in this order - always ask yourself
- what happens to my program if a client sends 2 GetReady requests? Will
it overwrite the stream-variable essentially making the old stream a
garbage object that is a memory leak?
And what happens if he disconnects without StreamDone? Be sure that the
stream is freed then as well.

johannes

Peter I. Dunne (Team GDA)

unread,
Mar 2, 2002, 11:26:16 AM3/2/02
to
I create the object in the OnConnect event so my code is simpler in the
OnExecute event
This also simplifies the management when the data is sent in more than one
event as is often the case
to create the object in the OnExecute event also forces us to check if it is
already assigned and slows the processing of the OnExecute event
If I want to process messages for 1 client no problem but what happens when
my server is connected to 400 clients?
you can see that the extra code will have an impact on performance!
johannes you have a very valid point about garbage, you can of course leave
it to the Thread.Destroy method to free the dataobject but..
I am using a idThreadPool manager and I want to minimize the memory overhead
of unused threads so I destroy the data object in the OnDisconnect event,
this also prevents memory leak when you use the OnConnect event to create
the dataobject as we will always start with a free AThread.Data pointer

--
Regards
Peter I. Dunne

http://www.gda.bizhosting.com (A developers site by developers for
developers)
No attachments in replies unless requested please

Johannes Berg wrote in message ...

Arthuro

unread,
Mar 3, 2002, 9:45:42 AM3/3/02
to
"Peter I. Dunne (Team GDA)" <refuge...@hotmail.com> schreef in bericht
news:3c80fd2c$1_2@dnews...

> I create the object in the OnConnect event so my code is simpler in the
> OnExecute event
> This also simplifies the management when the data is sent in more than one
> event as is often the case
> to create the object in the OnExecute event also forces us to check if it
is
> already assigned and slows the processing of the OnExecute event
> If I want to process messages for 1 client no problem but what happens
when
> my server is connected to 400 clients?
> you can see that the extra code will have an impact on performance!

This is very true, i didn't think it through that far..

> johannes you have a very valid point about garbage, you can of course
leave
> it to the Thread.Destroy method to free the dataobject but..
> I am using a idThreadPool manager and I want to minimize the memory
overhead
> of unused threads so I destroy the data object in the OnDisconnect event,
> this also prevents memory leak when you use the OnConnect event to create
> the dataobject as we will always start with a free AThread.Data pointer

You will have memory leaks if you do it in this way.
OnDisconnect never happens if the client pulls the plugs, or the connection
is lost in another 'non' normal way.
This has always been a 'manco' in Indy in my view, and in the least it has
caused many problems for programmers
all around, the subject of OnDisconenct never happening has been raised many
many times over in this newsgroup alone..

If in the overly-simple example i gave:

>> Get Ready
>> SendStream
>> StreamDone

the client pulls the plugs after SendStream, you're still waiting for the
streamdone command..
Using OnDisconnect won't help because it never fires server side, only if
you explicitly call disconnect yourself, and that
won't happen since you're still waiting for the streamdone..
Of course you could implement it so that if the read times out, you can give
it a few retries and if the connection is really dead,
then eventually something is triggered, but it won't be OnDisconnect.


Marco..


Peter I. Dunne (Team GDA)

unread,
Mar 10, 2002, 3:34:43 PM3/10/02
to
Thanks Arthuro, now I gotta add more code :-( just when I thought I had it
licked
Useful info, keep up the good work

Arthuro wrote in message <3c823715_1@dnews>...

0 new messages