Solved

sending/receiving files (client/server sockets)

Posted on 1998-11-18
16
199 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now