Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

sending/receiving files (client/server sockets)

Posted on 1998-11-18
16
222 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 50 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Delphi: how to send PJL commands to printer 3 109
code issue 8 156
Delphi Form ownership 4 113
TEMBEDDEDWB how can i change its user agent ? 8 70
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 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