//服务端
unit s;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls, ComCtrls,math,
IdAntiFreezeBase, IdAntiFreeze, ExtCtrls;
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
StatusBar1: TStatusBar;
IdAntiFreeze1: TIdAntiFreeze;
ListView1: TListView;
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
private
AFileStream: TFileStream;
{ Private declarations }
public
{ Public declarations }
end;
type
TSimpleClient = Record
id: longint;
FileName: string;
Filesize: integer;
Sendsize: integer;
IP: string;
Port: integer;
end;
PClient = ^TSimpleClient;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
cmd: string;
ASize: Integer;
Client: PClient;
i,index:integer;
begin
with AThread.Connectiondo
begin
athread.Synchronize;
Client := Pointer(AThread.Data);
index:=-1;
for i:=0 to listview1.Items.Count -1do
if strtoint(listview1.items.SubItems [4])=client.id then
index:=i;
cmd := UpperCase(ReadLn);
if copy(cmd,0,5) = 'begin
' then
begin
AFileStream := TFileStream.Create('E:/'+copy(cmd,6,100), fmOpenRead);
Client.FileName := copy(cmd,6,100);
Client.FileSize := AFileStream.Size;
listview1.Items[index].caption:=client.FileName;
listview1.Items[index].subitems[0]:=client.IP ;
listview1.Items[index].subitems[1]:=inttostr(client.port);
listview1.Items[index].subitems[2]:=formatfloat('0.000',(AFileStream.Size /1024 / 1024))+'M';
Exit;
end;
if cmd = 'END' then
begin
listview1.Items[index].Delete;
Exit;
end;
if cmd = 'CANCEL' then
begin
listview1.items[index].Delete;
Exit;
end;
AFileStream.Seek(StrToInT(cmd), soFrombegin
ning);
ASize := Min(AFileStream.Size - AFileStream.Position, RecvBufferSize);
OpenWriteBuffer;
WriteStream(AFileStream, false, false, ASize);
Client.SendSize := strtoint(cmd);
CloseWriteBuffer;
try listview1.Items[index].SubItems[3]:=formatfloat('0%',strtofloat(cmd) / client.filesize *100);except end;
end;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
IdTCPServer1.Active := False;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
if not IdTCPServer1.Active then
IdTCPServer1.Active := True;
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
var Client: PClient;
begin
Client := new( PClient );
Client.id := listview1.Items.Count+1 ;
Client.FileName := '';
Client.FileSize := 0;
Client.SendSize := 0;
Client.IP := AThread.Connection.Socket.Binding.PeerIP;
Client.Port := AThread.Connection.Socket.Binding.PeerPort;
AThread.Data := Pointer(client );
with listview1.items.Adddo
begin
caption:='';
subitems.add('');
subitems.add('');
subitems.add('');
subitems.add('');
subitems.add(inttostr(client.id));
end;
end;
procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
var Client: PClient;
begin
Client := Pointer(AThread.Data);
AThread.Data := nil;
end;
end.