Solved

send File over clientsocket and serversocket

Posted on 2002-07-22
6
2,886 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
Comment Utility
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
Comment Utility
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
Comment Utility
.. faster ..... ^_^; waited 5 days
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Akazukin
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

10 Experts available now in Live!

Get 1:1 Help Now