delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (18)


A simple way to send files using TClientSocket & TServerSocketFormat this article printer-friendly!Bookmark function is only available for registered users!
Sending files using the standard Delphi client/server socket components.
Product:
Delphi all versions
Category:
Communication
Skill Level:
Scoring:
Last Update:
05/29/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet ClientSocket ServerSocket File Sending
Times Scored:
32
Visits:
29290
Uploader: M K
Company:
Reference: Friend's Example, refined by me.
 
Question/Problem/Abstract:
How can I send files using TClientSocket & TServerSocket?
Sending files with TClientSocket/TServerSocket
Answer:



unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ScktComp, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Image2: TImage;
    ClientSocket1: TClientSocket;
    ServerSocket1: TServerSocket;
    Button1: TButton;
    procedure Image1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ClientSocket1Connect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocket1ClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
  private
    { Private declarations }
    Reciving: boolean;
    DataSize: integer;
    Data: TMemoryStream;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Image1Click(Sender: TObject);
begin
  // This is the procedure to open the socket for RECEIVING.
  // Button1.Click is this procedure as well.
  ClientSocket1.Active:= true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Open the SENDING socket.
  ServerSocket1.Active:= true;
end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  // Send command to start the file sending.
  Socket.SendText('send');
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
var
  s, sl: string;
begin
  s:= Socket.ReceiveText;
  // If we're not in recieve mode:
  if not Reciving then
  begin
    // Now we need to get the length of the data stream.
    SetLength(sl, StrLen(PChar(s))+1); // +1 for the null terminator
    StrLCopy(@sl[1], PChar(s), Length(sl)-1);
    DataSize:= StrToInt(sl);
    Data:= TMemoryStream.Create;
    // Delete the size information from the data.
    Delete(s, 1, Length(sl));
    Reciving:= true;
  end;
  // Store the data to the file, until we've received all the data.
  try
    Data.Write(s[1], length(s));
    if Data.Size = DataSize then
    begin
      Data.Position:= 0;
      Image2.Picture.Bitmap.LoadFromStream(Data);
      Data.Free;
      Reciving:= false;
      Socket.Close;
    end;
  except
    Data.Free;
  end;
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  ms: TMemoryStream;
begin
  // The client wants us to send the file.
  if Socket.ReceiveText = 'send' then
  begin
    ms:= TMemoryStream.Create;
    try
      // Get the data to send.
      Image1.Picture.Bitmap.SaveToStream(ms);
      ms.Position:= 0;
      // Add the length of the data, so the client
      // will know how much data to expect.
      // Append #0 so it can determine where the size info stops.
      Socket.SendText(IntToStr(ms.Size) + #0);
      // Send it (the socket owns the stream from now on).
      Socket.SendStream(ms);
    except
      // So only free the stream if something goes wrong.
      ms.Free;
    end;
  end;
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
help me
    gabi buda (Feb 9 2008 2:34PM)

hello, i am a beginner in delphi programming and i want to send me an archive on my mail (gabitza_bm@yahoo.com) with the code if you can because i get some errors... thanks
Respond

Any body can help me
    ali adlah (Sep 28 2007 8:42PM)

How i can send Screen Shot from server socket to client socket
Respond

RE: Any body can help me
ali karimi (Dec 22 2007 3:43PM)

hi love u from italy
Respond

Cancel transfer?
    Wack aMole (Nov 13 2006 7:28PM)

So i'm sending a very big file (100mbs) with Socket.SendStream.

My question is, how can I cancel the transfer while it is going on?
Respond

RE: Cancel transfer?
Ireene Muhajirin (Nov 18 2006 10:26AM)

It's the best for our information,because this programm can solving the problem in IT.
Respond

RE: RE: Cancel transfer?
Ireene Muhajirin (Nov 19 2006 9:41AM)

It's the best for our information,because this programm can solving the problem in IT.

Respond

Send image
    cfh (Nov 24 2005 12:27PM)

How to send an image from TClientSocket to TServerSocket1.

Respond

RE: Send image
Ireene Muhajirin (Nov 18 2006 10:16AM)

The program it's very needed for Finally Project, so I want to request this program for that.
Respond

RE: Send image
ali adlah (Sep 28 2007 2:48AM)

Thank you for this responde
Respond

Client/Server Socket violation!!
    Stephanie (Apr 15 2005 7:16PM)

I have tried to run the code in this article and i get the following error:

No Address Specified,

Help, i've tried numerous things but can't get it to work
Respond

RE: Client/Server Socket violation!!
Derli Ferreira (Dec 2 2005 2:00PM)

Sure it will not work with Out an Ip adrress, the Client Socket must to have and adress to attempt to conect to the server socket that is activing listening for the same port that the client is tring to conecct.

  try with yours loop back adress,
  ClientSocket1.adress:='127.0.0.1';

Respond

large files
    Jennifer Langley (Aug 20 2004 12:27AM)

when I try to send a large file such as a video clip it gives me an error and says out of memory. how can i fix this please?
Respond

Memory Eater
    Allan Mackinnon (Dec 14 2001 4:42PM)

Greetings,

I am using a similar method to stream data from a server to a client on a regular basis, but have found the following problem,

Each time the server socket is given the stream of data to send, it is not disposing of the data properly after the stream has been sent.
After a day of this, the computer will have no memory to use, and fail.
I cant force the stream to be disposed of in any way because the socket has possession of it.

the second time the procedure is called the stream is not = nil.
I tried forcing it to reuse the same memory stream by only creating it if it was = nil.
However, when I try to write to the stream a second time, without recreating it, i get an access violation, because the socket has possession.

Any idea how I can resolve this issue?

Thanks for your time,
Allan.

Respond

diferences between non-blockig and thread-blocking
    Rudy Lay (Jun 21 2001 12:53AM)

What's the server type that is used within this program? can anybody tell me what the differences between 'stNon-Blocking' and 'stThread-Blocking' are? and how to use the 'stThread-Blocking' server type?
Thank you,
Rudy L.
Respond

RE: diferences between non-blockig and thread-blocking
M K (Jun 23 2001 7:09AM)

Non blocking server is used in this example. A full explanation about hte difference is in the Delphi Help file ... look under blocking connections ---> connections ....
Respond

ProgressBar? (data received)
    Dennis (May 30 2001 9:19AM)

Is there a easy way to check how much of the file received, and how much the file is on??
I will use it to get a ProgressBar show the filetransfer progress.


Dennis
Respond

RE: ProgressBar? (data received)
LinK SoLE (Jun 11 2001 12:32AM)

the c0de seems ok but dosnt runs on delphi4
Respond

RE: ProgressBar? (data received)
Mike Kniec (Feb 27 2007 2:48PM)

Put in "try" section of recieving file something like this:

ProgressBar.Max := DataSize;

Should work fine.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


   


  Community Ad of
M. Maes
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)