unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, ImgList, ComCtrls, Menus;
type
TForm1 =
class(TForm)
ImageList1: TImageList;
memLog: TMemo;
ServerSock: TServerSocket;
TransferSock: TServerSocket;
Button1: TButton;
ProgressBar: TProgressBar;
procedure ServerSockClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSockClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure ServerSockClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure TransferSockClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
IncommingStream: TFileStream;
Size: integer;
public
{ Public declarations }
TransferName:
String;
end;
var
Form1: TForm1;
implementation
uses Unit6;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSock.Socket.Connections[0].SendText('
000' + '
c:\meinedatei.txt');
TransferName := '
meinedatei.txt';
end;
procedure TForm1.ServerSockClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
begin
memLog.Lines.Add('
.:: Client ' + Socket.RemoteAddress + '
Disconnected');
end;
procedure TForm1.ServerSockClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
memLog.Lines.Add('
.:: Error ' + IntToStr(ErrorCode));
end;
procedure TForm1.ServerSockClientRead(Sender: TObject;Socket: TCustomWinSocket);
var
ReceivedText, cmd, data:
string;
i: integer;
Item: TListItem;
StrLst: TStringList;
begin
ReceivedText := Socket.ReceiveText;
cmd := Copy(ReceivedText, 0, 3);
data := Copy(ReceivedText, 4, Length(ReceivedText));
if cmd = '
000'
then
begin
memLog.Lines.Add(data);
end;
if cmd = '
001'
then
begin
memLog.Lines.Add('
.:: Client connected: ' + data + '
on ' + socket.RemoteAddress);
end;
if cmd = '
002'
then
begin
Size := StrToInt(Data);
Form1.ProgressBar.Min := 0;
Form1.ProgressBar.Max := Size;
memLog.Lines.Add('
.:: Receiving file of size ' + Data + '
bytes');
IncommingStream := TFileStream.Create(Form1.TransferName, fmCREATE
or fmOPENWRITE
and fmsharedenywrite);
TransferSock.Open;
end;
end;
procedure TForm1.TransferSockClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
Buffer:
array [0..9999]
of Char;
IncommingLen, RecievedLen: integer;
begin
IncommingLen := socket.ReceiveLength;
while IncommingLen > 0
do
begin
RecievedLen := socket.ReceiveBuf(Buffer, Sizeof(Buffer));
if RecievedLen <= 0
then
Break
else
IncommingStream.
Write(Buffer, RecievedLen);
Form1.ProgressBar.StepBy(RecievedLen);
if IncommingStream.Size >= Size
then
begin
IncommingStream.Free;
ServerSock.Socket.Connections[0].SendText('
002');
Form1.ProgressBar.Position := 0;
if Form1.TransferName = '
Desktop.bmp'
then;
Break;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
memLog.Lines.Add('
Listening on Port ' + '
21');
ServerSock.Port := 21;
ServerSock.Open;
end;
end.