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
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
is there any particular reason why you went for http stuff instead of using
writefile on a tcpclient ?
Martin
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
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.
> 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
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>...
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]
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
--
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 ...
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..
Arthuro wrote in message <3c823715_1@dnews>...