Solved

send File over clientsocket and serversocket

Posted on 2002-07-22
6
2,950 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
[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
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
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!

 

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Delphi selector screen 2 97
delphi parse string to params 3 181
Delphi Seattle StructureView color 1 35
Delphi, TImage, Png 2 60
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

710 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