Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sending/receiving files (client/server sockets)

Posted on 1998-11-18
16
Medium Priority
?
269 Views
Last Modified: 2010-04-04
Hello, Can somebody can tell me how to send and receive files that are > 8k in size, with a TClientSocket and a TserverSocket? I would like send files of 2-3 megs in size.
A working example would be apprecieated.
0
Comment
Question by:treasure111898
  • 7
  • 4
  • 3
  • +2
16 Comments
 
LVL 4

Expert Comment

by:dwwang
ID: 1347249
I have a programm, but it's part of our software, and hard to seperate, I don't know how to help yo with that. Maybe you can give some suggestion?
0
 
LVL 1

Expert Comment

by:zac
ID: 1347250
Treasure,

Have a look at Q.10099120 "Remote screen monitoring".

In that question I outlined the method for transfering a large bitmap from one machine to another user tcp/ip.

The example used François PIETTE's socket component, but the procedure is much the same.

If you want any further assistance with it let me know

Good luck

Zac
0
 

Author Comment

by:treasure111898
ID: 1347251
I'm pretty new to sockets & delphi.  If at all possible could you post a small example that I can go through.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:treasure111898
ID: 1347252
To dwwang

In a previous ans I think you sent some code as an example. I know how to send a text file across the network.  I just need an example of how to send/receive large files(2-3 megs). As I am new to programming I would just like to see a working example of how to do this.
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1347253
Yes, our software can transfer really large files, over 15Meg bytes(and virtually any size).

However, I'll be on a trip tomorrow. If you can wait then after I get back I will arrange the code into an example and send to you.
0
 

Author Comment

by:treasure111898
ID: 1347254
To dwwang

That's really cool of you, I'll wait for you example.
0
 
LVL 1

Expert Comment

by:Romanian
ID: 1347255
Look answer "Send/Receive File with TClient/TServerSocket..." there you find sample code for your question

0
 

Author Comment

by:treasure111898
ID: 1347256
Thanks a lot Romanian
I think I'll wait for dwwang to send me his client/server socket example.





0
 
LVL 4

Expert Comment

by:dwwang
ID: 1347257
Thanks treasure :)

I'm still outside, maybe for another one week. So please be patient, sorry.
0
 
LVL 3

Accepted Solution

by:
williams2 earned 200 total points
ID: 1347258
Ok treasure. I got some working code for you!

Do the following before some cut'n'paste jobs..

Part A (Client):
Start up a new delphi application.
Drag two Buttons, a TOpenDialog and a TClientSocket component onto your form.
1. create an event procedure for buttonclick on both buttons.
2. create an event procedure for OnConnect,OnError and OnRead.
3. Remeber to save the whole thing in some directory, or Delphi won't let you compile project from Part B.

Cut'n'Paste the following lines to Unit1:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    ClientSocket1: TClientSocket;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure ClientSocket1Connect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
      ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
  private
    { Private declarations }
    Stream: TMemoryStream;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  ClientSocket1.Address:= '127.0.0.1';
  ClientSocket1.Port:= 2500;
  ClientSocket1.Open;
end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  ShowMessage('Connected.. Now go load a file!');
end;

procedure TForm1.ClientSocket1Error(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
  ShowMessage('Did you startup the server? I cannot find it!');
end;


procedure TForm1.Button2Click(Sender: TObject);
var
  Size: Integer;
begin
  if OpenDialog1.Execute Then
  begin
    Stream.LoadFromFile(OpenDialog1.Filename);
    Size:= Stream.Size;
    ClientSocket1.Socket.SendBuf(Size,SizeOf(Size));
    ClientSocket1.Socket.SendStream(Stream);
  End;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  Stream:= TMemoryStream.Create;
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
var
  S: String;
begin
  S:= Socket.ReceiveText;
  Socket.Close;
  ShowMessage('Client: '+S);
end;

end.

____________________________________________________________________________

Part B (Server): Now to some server things..

Start up a new delphi application.
Drag an TSaveDialog and a TServerSocket component onto your form.
1. create an event procedure for OnClientConnect, OnClientRead, OnListen.
2. create an event procedure for OnConnect,OnError and OnRead.

Cut'n'Paste the following lines to Unit1:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ServerSocket1: TServerSocket;
    SaveDialog1: TSaveDialog;
    procedure FormCreate(Sender: TObject);
    procedure ServerSocket1ClientConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocket1Listen(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocket1ClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
  private
    { Private declarations }
    Stream: TMemoryStream;
    FSize: Integer;
    writing: Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ServerSocket1.Port:= 2500;
  ServerSocket1.Active:= True;
  Stream:= TMemoryStream.Create;
  writing:= False;
end;

procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  ShowMessage('A client has connected');
end;

procedure TForm1.ServerSocket1Listen(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  ShowMessage('I''m listening');
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  BytesReceived: Longint;
  CopyBuffer: Pointer; { buffer for copying }
  ChunkSize: Integer;
  TempSize: Integer;
const
  MaxChunkSize: Longint = 8192; { copy in 8K chunks }
begin
  If FSize=0 then
  begin
    If Socket.ReceiveLength>SizeOf(TempSize) then
    begin
      Socket.ReceiveBuf(TempSize,SizeOf(TempSize));
      Stream.SetSize(TempSize);
      FSize:= TempSize //Threadsafe code!
    End;
  End;
  If (FSize>0) and not(writing) then
  begin
    GetMem(CopyBuffer, MaxChunkSize); { allocate the buffer }
    writing:= True;
    While Socket.ReceiveLength>0 do
    Begin
      ChunkSize:= Socket.ReceiveLength;
      If ChunkSize > MaxChunkSize then ChunkSize:= MaxChunkSize;
      BytesReceived:= Socket.ReceiveBuf(CopyBuffer^,ChunkSize);
      Stream.Write(CopyBuffer^, BytesReceived); { ...write chunk }
      Dec(FSize,BytesReceived);
    End;
    If FSize=0 then
    If SaveDialog1.Execute then
    begin
      If FileExists(SaveDialog1.Filename) then
        DeleteFile(SaveDialog1.Filename);
      Stream.SaveToFile(SaveDialog1.Filename);
      Socket.SendText('File received!');
      Stream.SetSize(0);
      FSize:= 0;
    End;
    FreeMem(CopyBuffer, MaxChunkSize); { allocate the buffer }
    Writing:= False;
  End;
end;


end.

0
 
LVL 3

Expert Comment

by:williams2
ID: 1347259
..I forgot to mention the Formcreate event on both units :-)

If you want me to explain the whole thing, You might consider raising the credits, but it works!
0
 

Author Comment

by:treasure111898
ID: 1347260
Thanks a lot William2.  Your solution is just the example I'm looking for.

I am still hoping to view dwwang example. If it is still possible?
0
 
LVL 3

Expert Comment

by:williams2
ID: 1347261
If you need more help, then just say so. I've been working around this subject for some time now, so I feel pretty much safe in the client/server area
0
 

Author Comment

by:treasure111898
ID: 1347262
Thanks a lot william2.  I'm glad that there are Delphi gurus out there like yourself willing to help.  How do I get your email William2?
0
 
LVL 3

Expert Comment

by:williams2
ID: 1347263
You can contact me at Williams90@hotmail.com. I will reply to you with my personal e-mail.
0
 

Author Comment

by:treasure111898
ID: 1347264
Thanks Williams2.  
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question