Solved

send File over clientsocket and serversocket

Posted on 2002-07-22
6
2,979 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
Independent Software Vendors: 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

631 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