Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sending/receiving files (client/server sockets)

Posted on 1998-11-18
16
Medium Priority
?
264 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
[X]
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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

721 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