Link to home
Start Free TrialLog in
Avatar of CtrlShft
CtrlShft

asked on

help with BlockRead BlockWrite ScktComp

I finally finish my demo of transfer files over a network but the code dont work, someone can tell me what im doing wrong?
Client
 
unit UntMain;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ScktComp, StdCtrls, Menus;
 
type
  TForm1 = class(TForm)
    Open: TOpenDialog;
    Client: TClientSocket;
    PopupMenu1: TPopupMenu;
    SendData1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure SendData1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.FormCreate(Sender: TObject);
begin
 Client.Open;
end;
 
procedure SendFile(FilePath: String; Socket: TCustomWinSocket);
var
 F: File;
 NumRead: Integer;
 Buffer: array [1..4096] of Char;
begin
 AssignFile(F, FilePath);
 Reset(F, 1) ;
 repeat
   BlockRead(F, Buffer, SizeOf(Buffer), NumRead);
   Socket.SendBuf(Buffer, NumRead);
  until (NumRead = 0);
  CloseFile(F) ;
end;
 
 
procedure TForm1.SendData1Click(Sender: TObject);
begin
 Client.Socket.SendText('FILE');
 SendFile('C:\PortScan.exe', Client.Socket);
end;
 
end.
 
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 
Server
 
unit UntMain;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ScktComp;
 
type
  TForm1 = class(TForm)
    Server: TServerSocket;
    procedure FormCreate(Sender: TObject);
    procedure ServerClientRead(Sender: TObject; Socket: TCustomWinSocket);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  Server.Open;
end;
 
procedure GetFile(FilePath: string; Socket: TCustomWinSocket);
var
  F: file;
  NumRead, NumWritten: Integer;
  Buffer: array[1..4096] of Char;
begin
  AssignFile(F, FilePath);
  ReWrite(F, 1);
  repeat
    Socket.RecvBuf(Buffer, NumRead);
    BlockWrite(F, Buffer, NumRead, NumWritten);
  until (NumWritten <> NumRead);
  CloseFile(F);
end;
 
procedure TForm1.ServerClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  Data: string;
begin
  Data := Socket.RecvText;
  if Data = 'FILE' then
  begin
    GetFile('File.exe', Socket);
  end;
end;
 
end.

Open in new window

Avatar of 8080_Diver
8080_Diver
Flag of United States of America image

In what way does the code not work?  Are there error messages?  Have you stepped through it in debug mode?
ASKER CERTIFIED SOLUTION
Avatar of JonasMalmsten
JonasMalmsten
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CtrlShft
CtrlShft

ASKER

OK JonasMalmsten, i modified a lite bit, here is the new code:

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
 Client.Host:= 'localhost';
 Client.Port:= 1;
 Client.Open;
end;

procedure SendFile(FilePath: String);
var
 F: File;
 NumRead: Integer;
 Buffer: array [1..4096] of Char;
begin
 AssignFile(F, FilePath);
 Reset(F, 1) ;
 repeat
   Application.ProcessMessages;
   BlockRead(F, Buffer, SizeOf(Buffer), NumRead);
   Form1.Client.Socket.SendBuf(Buffer, NumRead);
  until (NumRead = 0);
  CloseFile(F) ;
end;


procedure TForm1.SendData1Click(Sender: TObject);
begin
 SendFile('C:\PortScan.exe');
end;

Server

{$R *.dfm}

procedure GetFile(FilePath: string);
var
  F: file;
  NumRead, NumWritten: Integer;
  Buffer: array[1..4096] of Char;
begin
  AssignFile(F, FilePath);
  ReWrite(F, 1);
  repeat
    Application.ProcessMessages;
    Form1.Server.Socket.RecvBuf(Buffer, NumRead);
    BlockWrite(F, Buffer, NumRead, NumWritten);
  until (NumWritten <> NumRead);
  CloseFile(F);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Server.Port:= 1;
  Server.Open;
end;

procedure TForm1.ServerClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
begin
 GetFile('File.exe');
end;

pls tell me what you think, when i send the data from the client to the server i get error "Asynchronous Socket Error 10053 "
so where i can place the GetFile procedure, because i try it on a timmer:
if Server.Socket.Connected the GetFile('File.exe');
and dont work.