Solved

send File over clientsocket and serversocket

Posted on 2002-07-22
6
2,924 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

821 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