RTC Forums
April 29, 2024, 04:30:52 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: need example - client server upload jpg with result  (Read 4451 times)
karen
Guest
« on: February 08, 2010, 01:32:50 PM »

Hi - I've searched quick start, demo and read forum but not found exact example I need to [1] create a RTCclient to send a jpg to a RTCserver [2] RTCserver receives jpg file, saves file and processes it in a certain way [3] RTCserver sends result ( just in text form e.g. 'yes' or 'no' ) of this process to RTCclient. Forgive me if the examples already show this, in that case please say which examples. I have tried quickstart\client upload example - but I do not see how server sends result from that and how/where/when client can respond after sending the jpg. Any guidance will be appreciated. Smiley
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: February 08, 2010, 02:26:46 PM »

The Server always sends a response to a client and the client will always receives a response. The Server will send a standard "200 OK" response without any other content if you have accepted a request from the Client but have not manually prepared a response. And the client will simply ignore the response if you do not implement its OnDataReceived event.

To process the response received from the Server, you need to implement the OnDataReceived event on the TRtcDataRequest component (Client) which has sent the request. In the rtcClientUpload quick start example, this will be the rtcDataRequest1 component on the main form, which currently does NOT have an implementation for the OnDataReceived event but instead uses the OnResponseDone event because it does not care about the status.

To extend the rtcClientUpload example so it will accept and check the response from the Server, remove the current implementation for the OnResponseDone event (delete the line of code inside RtcDataRequest1ResponseDone event) and implement the OnDataReceived event on the same component like this:

procedure TForm1.RtcDataRequest1DataReceived(Sender: TRtcConnection);
  begin
  with TRtcDataClient(Sender) do
    begin
    { We do not expect a long response,
      so we can wait for the response to be Done before reading it. }
    if Response.Done then
      begin
      { We can use "Read" here to get the complete content sent from the Server
        if we expect the Server to send a short content. If the Server is expected
        to send a long response, you should use Read before Response.Done and
        write the read content to a file as it arrives to avoid flooding your memory. }
      btnPutFile.Caption:='Done, Status = '+
          IntToStr(Response.StatusCode)+' '+Response.StatusText;
      end;
    end;
  end;

And on the Server side, to process the received file and send a different response to the Client thank just a simple "200 OK", extend the OnDataReceived event on the TRtcDataProvider component (RtcDataProvider1 component on the rtcUploadServer projects main form), like this:

procedure TForm2.RtcDataProvider1DataReceived(Sender: TRtcConnection);
  var
    s:string;
  begin
  with TRtcDataServer(Sender) do
    begin
    if Request.Started then
      begin
      if not DirectoryExists(eUploadFolder.Text) then
        CreateDir(eUploadFolder.Text);
      Delete_File(Request.Info['file']);
      end;
    s:=Read;
    Write_File(Request.Info.asString['file'], s, Request.ContentIn-length(s));

    if Request.Complete then
      begin
      { We have the complete request content here (the complete file).
        We can could process the file (now stored locally) and send a response
        to the Client letting the client know how our processing went. }
      Response.Status(200,'OK'); // Set response status to a standard "OK"
      Write('Thanks for the file.'); // Send a short message to let the client know how we did.
      end;
    end;
  end;

Please note that this is under the presumption that you only want to send a short response from the Server. If you want to send a file back from the Server (which you can also do, ofcourse), you will have to combine the File Upload example with a File Download example.

The events you will need for sending a longer file are OnDataReceived for preparing the Response Status, Response Headers and setting the ContentLengt before sending the response and using WriteHeader to start the send process. And you will need the OnDataSent event on the Server to send your long content out chunk-by-chunk to avoid flooding its memory, just like the Client is doing it now in the Upload file example. The client will then read the file chunk-by-chunk from its OnDataReceived event, very similar to how the Server is doing it in its OnDataReceived event.

The main difference between Client and Server side is that the Server uses the Request object to receive data, while the Client uses the Request object to prepare a request before sending data, and then the Server uses the Response object to prepare data for sending so the Client will use the Response object to read data received from the Server.

I hope this helps.

Best Regards,
Danijel Tkalcec
Logged
karen
Guest
« Reply #2 on: February 09, 2010, 01:14:02 AM »

 Smiley Thank you !
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.023 seconds with 16 queries.