Solved

send File over clientsocket and serversocket

Posted on 2002-07-22
6
2,916 Views
Last Modified: 2012-08-13
Chat:

Hi i am using Delphi 3 and trying to create a program to
transfer files using client and server sockets. However it gives error messages after 8192 bytes of transfer for a 1mb file. i am new to delphi so please help me and guide me with examples, on how to solve this problem. I have used my beginning 75 points to view other's questions but they seems difficult for me to learn. i shall increase award to 100 points after 10 days when i receive the points. thanks

-------------------

Code:

procedure TForm1.Button4Click(Sender: TObject);
begin
serverSocket1.socket.connections[0].SendStream(TFileStream.Create(edit3.text, fmOpenRead or fmShareDenyWrite));
end;


procedure TForm1.ClientSocket1Read(Sender: TObject;
 Socket: TCustomWinSocket);
var
 iLen: Integer;
 Bfr: Pointer;
begin
iLen := Socket.ReceiveLength;
 GetMem(Bfr, iLen);
 try
   Socket.ReceiveBuf(Bfr^, iLen);
   FStream.Write(Bfr^, iLen);
 finally
   FreeMem(Bfr);
   FStream.Free;
   FStream := nil;
 end;
end;

---------------------------
Signiture

Yours, Irene (beginner)
0
Comment
Question by:Akazukin
6 Comments
 
LVL 1

Accepted Solution

by:
mpoots earned 170 total points
ID: 7171763
Hi Irene,

I have not tested your code but reading from it I think it can never work.

FStream.Write(Bfr^, iLen);

You have never declared FStream. If you want to work with client/server components I would advise you to use the Free  Indy components. Since version Delphi 6 Indy is included in Delphi but in versions before that you have to download it from here: http://www.nevrona.com/Indy/

Marcel
0
 

Author Comment

by:Akazukin
ID: 7172426
Chat:

Dear Marcel i am using delphi 3, i think i might get errors if i download Indy Delphi 6. hope you can help me to test the code and solve the problem, below is the full source code. *smiles in real :)*
-----------------------------------------------

unit test;

interface

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

type
  TForm1 = class(TForm)
    ClientSocket1: TClientSocket;
    ServerSocket1: TServerSocket;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Button3: TButton;
    Edit2: TEdit;
    Button4: TButton;
    Edit3: TEdit;
    Button5: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure ClientSocket1Connect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocket1Accept(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  private
  FStream: TFileStream;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  X,Y,Z: integer;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
clientsocket1.Host := edit1.text;
clientsocket1.active := true;
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
var
  iLen: Integer;
  Bfr: Pointer;
begin
iLen := Socket.ReceiveLength;
form1.caption := 'X is '+inttostr(iLen)+' Y is '+inttostr(Y);
  GetMem(Bfr, iLen);
  try
    Socket.ReceiveBuf(Bfr^, iLen);
    FStream.Write(Bfr^, iLen);
  finally
    FreeMem(Bfr);
    FStream.Free;
    FStream := nil;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
serversocket1.active := true;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
FStream := TFileStream.Create(edit2.text, fmCreate or fmShareDenyWrite);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
serverSocket1.socket.connections[0].SendStream(TFileStream.Create(edit3.text, fmOpenRead or fmShareDenyWrite));
end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
form1.caption := 'connected';
end;

procedure TForm1.ServerSocket1Accept(Sender: TObject;
  Socket: TCustomWinSocket);
begin
form1.caption := 'connected';
end;

procedure TForm1.Button5Click(Sender: TObject);
var f: file of Byte;
   size : Longint;
begin
AssignFile(f, edit3.text);
Reset(f);
size := FileSize(f);
form1.Caption := 'File size in bytes: ' + IntToStr(size);
serversocket1.socket.connections[0].sendtext(inttostr(size));
showmessage('File size is '+ (inttostr(size div 1024)) + ' kbytes');
CloseFile(f);
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
edit3.text := 'C:\test.bmp';
end;

function getIPs: Tstrings;
type
  TaPInAddr = array[0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
var
  phe: PHostEnt;
  pptr: PaPInAddr;
  Buffer: array[0..63] of Char;
  I: Integer;
  GInitData: TWSAData;
begin
  WSAStartup($101, GInitData);
  Result := TstringList.Create;
  Result.Clear;
  GetHostName(Buffer, SizeOf(Buffer));
  phe := GetHostByName(buffer);
  if phe = nil then Exit;
  pPtr := PaPInAddr(phe^.h_addr_list);
  I    := 0;
  while pPtr^[I] <> nil do
  begin
    Result.Add(inet_ntoa(pptr^[I]^));
    Inc(I);
  end;
  WSACleanup;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.lines := getIPs;
edit1.text := memo1.lines[0];
X := 0;
Y := 0;
Z := 0;
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
edit3.text := 'C:\test2.bmp';
end;

procedure TForm1.Button8Click(Sender: TObject);
begin
edit3.text := 'C:\akazukin.bmp';
end;

end.

------------------------------------
Signiture

Yours, Irene (beginner)
0
 

Author Comment

by:Akazukin
ID: 7179572
.. faster ..... ^_^; waited 5 days
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Akazukin
ID: 7231112
why no answer? was that suppose to be hard?
i just hope someone show me how to send a file from client to server using delphi 3. the data is more then 8k. i've waited for a month.

>:(
0
 

Expert Comment

by:CleanupPing
ID: 9343016
Akazukin:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 9461386
Akazukin,
No comment has been added lately (18 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Award points to mpoots http:#7171763

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

anAKiN
EE Cleanup Volunteer
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 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