Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file transfer using tclientsocket and tserversocket

Status
Not open for further replies.

AbbasAkhtar

Programmer
Dec 16, 2002
25
GB
can anyone tell me if this code works, ive tried it and works for a almost 3 megabyte bmp file, works fine, ive tried it, offline only though, someone sed it does not work for larger files, but i dont no if he means online or offline ? can someone test this code please, and if it doesnt work for large files, e.g. 4/5 mb, can someone try to fix this ? or any solutions ?

also id like to use ics, but i cant find any samples for a direct file transfer, i dont wuna use ftp either, anyonc got any good samples, of file transfer, dont say "ics" cuz it dont come with file transfer samples, thanks,

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;

type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
btnTestSockets: TButton;
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure btnTestSocketsClick(Sender: TObject);
private
FStream: TFileStream;
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

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);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FStream := nil;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(FStream) then
begin
FStream.Free;
FStream := nil;
end;
end;

procedure TForm1.ClientSocket1Disconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
if Assigned(FStream) then
begin
FStream.Free;
FStream := nil;
end;
end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
FStream := TFileStream.Create('c:\temp\test.stream.html', fmCreate or fmShareDenyWrite);
end;

procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Socket.SendStream(TFileStream.Create('c:\temp\test.html', fmOpenRead or fmShareDenyWrite));
end;

procedure TForm1.btnTestSocketsClick(Sender: TObject);
begin
ServerSocket1.Active := True;
ClientSocket1.Active := True;
end;

end.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top