unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ScktComp, StdCtrls;
type
TForm1 =
class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Connected (Sender: TObject; Socket: TCustomWinSocket);
procedure ServerSocket1ClientConnected (Sender: TObject; Socket: TCustomWinSocket);
procedure ClientSocket1OnRead (Sender: TObject; Socket: TCustomWinSocket);
procedure ClientSocket1Disconnect(Sender: TObject; Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect (Sender: TObject; Socket: TCustomWinSocket);
private
{ Private-Deklarationen }
FStream: TFileStream;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
Dateiname :
String;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
FStream :=
nil;
RStream :=
nil;
ServerSocket1 := TServerSocket.create (self);
ClientSocket1 := TClientSocket.create (self);
ServerSocket1.OnClientConnect := ServerSocket1ClientConnected;
ServerSocket1.OnClientDisconnect := ServerSocket1ClientDisconnect;
ClientSocket1.OnConnect := ClientSocket1Connected;
ClientSocket1.OnRead := ClientSocket1OnRead;
ClientSocket1.OnDisconnect := ClientSocket1Disconnect;
ServerSocket1.Port := 1080;
ClientSocket1.Port := 1080;
ClientSocket1.Host := '
127.0.0.1';
end;
procedure TForm1.ServerSocket1ClientDisconnect (Sender: TObject; Socket: TCustomWinSocket);
begin
if Assigned(FStream)
then
begin
FStream.Free;
FStream :=
nil;
end;
end;
procedure TForm1.ClientSocket1OnRead (Sender: TObject; Socket: TCustomWinSocket);
var
iLen: Integer;
Bfr: Pointer;
begin
iLen := Socket.ReceiveLength;
GetMem(Bfr, iLen);
try
Socket.ReceiveBuf(Bfr^, iLen);
RStream.
Write(Bfr^, iLen);
finally
FreeMem(Bfr);
end;
end;
procedure TForm1.ClientSocket1Connected (Sender: TObject; Socket: TCustomWinSocket);
begin
RStream := TFileStream.Create(Dateiname+'
_send', fmCreate
or fmShareDenyWrite);
end;
procedure TForm1.ServerSocket1ClientConnected (Sender: TObject; Socket: TCustomWinSocket);
begin
Socket.SendStream(TFileStream.Create(Dateiname, fmOpenRead
or fmShareDenyWrite));
end;
procedure TForm1.ClientSocket1Disconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
if Assigned(FStream)
then
begin
FStream.Free;
FStream :=
nil;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.execute
then
begin
Dateiname := OpenDIalog1.Filename;
ServerSocket1.Active := True;
ClientSocket1.Active := True;
end;
end;
end.