下面是源代码,服务器和客户端在一个UNIT1中,经测试可用,你研究一下,不一定能对得上你的INDY版本.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdTCPServer, StdCtrls, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, ComCtrls, IdAntiFreezeBase, IdAntiFreeze;
type
TForm1 = class(TForm)
IdTCPClient1: TIdTCPClient;
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
Button3: TButton;
Label2: TLabel;
Edit2: TEdit;
Button4: TButton;
OpenDialog1: TOpenDialog;
IdTCPServer1: TIdTCPServer;
SaveDialog1: TSaveDialog;
Button5: TButton;
Button6: TButton;
Label3: TLabel;
ProgressBar1: TProgressBar;
IdAntiFreeze1: TIdAntiFreeze;
Button7: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure Button5Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure FormCreate(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
//fsSend: TFileStream;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
xcfilename:string;
xcsavestream:tfilestream;
xcpos:integer;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
IdTCPServer1.Active := True;
Button1.Enabled := False;
Button2.Enabled := False;
listbox1.Items.Add('服务器已成功启动!');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Edit1.Text='' then
begin
ShowMessage('服务器IP不能为空');
Exit;
end;
IdTCPClient1.Host:=edit1.Text;
listbox1.Items.Add('正在连接远程主机IP: ' + Edit1.Text );
with IdTCPClient1 do
begin
try
Connect;
try
listbox1.Items.Add(ReadLn());
Button1.Enabled := False;
except
listbox1.Items.Add('远程主机无响应!');
IdTCPClient1.Disconnect();
end;
except
listbox1.Items.Add('无法建立到远程主机' + Edit1.Text + '的连接!');
end;//end try
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
listbox1.Items.Add('QUIT');
try
IdTCPClient1.WriteLn('QUIT');
finally
IdTCPClient1.Disconnect();
listbox1.Items.add('同主机 ' + Edit1.Text + ' 的连接已断开!');
Button1.Enabled := True;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
opendialog1:=topendialog.Create(self);
if opendialog1.Execute then
begin
edit2.Text:=opendialog1.Filename;
button5.Enabled:=true;
end;
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
IdTCPServer1.Active := False;
idtcpclient1.Disconnect;
form1.Close;
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
listbox1.Items.Add('来自主机 '+ AThread.Connection.Socket.Binding.PeerIP+ ' 的连接请求已被接纳!');
AThread.Connection.WriteLn('欢迎连接到简单TCP服务器');
end;
procedure TForm1.Button5Click(Sender: TObject);
var
buf:array[0..1023] of byte;
filename,state:string;
filelen,readcount,pos:integer;
openstream:tfilestream;
begin
openstream:=tfilestream.Create(opendialog1.FileName,fmopenread or fmShareDenyWrite);
filename:=ExtractFileName(opendialog1.FileName);
idtcpclient1.WriteLn(filename);
state:=IdTCPClient1.ReadLn();
if state<>'xc' then
begin
try
listbox1.Items.Add('开始传送文件');
filelen:=openstream.Size;
idtcpclient1.WriteBuffer(filelen,4);
progressbar1.Max:=filelen;
while openstream.Position<openstream.Size do
begin
if openstream.Size-openstream.Position>=sizeof(buf) then
readcount:=sizeof(buf)
else
readcount:=openstream.Size-openstream.Position;
openstream.ReadBuffer(buf,readcount);
idtcpclient1.WriteBuffer(buf,readcount);
progressbar1.Position:=openstream.Position;
idtcpclient1.ReadLn(#13#10, 100);
Application.ProcessMessages;
end;
listbox1.Items.Add('文件传送完毕');
finally
openstream.Free;
end
end;
if state='xc' then
begin
try
listbox1.Items.Add('开始续传文件'+opendialog1.FileName);
filelen:=openstream.Size;
idtcpclient1.WriteBuffer(filelen,4);
idtcpclient1.ReadBuffer(pos,4);
openstream.Position:=pos;
progressbar1.Max:=filelen;
while openstream.Position<openstream.Size do
begin
if openstream.Size-openstream.Position>=sizeof(buf) then
readcount:=sizeof(buf)
else
readcount:=openstream.Size-openstream.Position;
openstream.ReadBuffer(buf,readcount);
idtcpclient1.WriteBuffer(buf,readcount);
progressbar1.Position:=openstream.Position;
idtcpclient1.ReadLn(#13#10, 100);
Application.ProcessMessages;
end;
listbox1.Items.Add('文件传送完毕');
finally
openstream.Free;
end
end;
end;
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
buf:array[0..1023] of byte;
savestream: TFileStream;
filename:string;
filelen,ReadCount:Integer;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
filename:=AThread.Connection.ReadLn();
if filename='' then
exit;
if filename<>xcfilename then
begin
savedialog1.FileName:=filename;
if savedialog1.Execute then
begin
AThread.Connection.WriteLn('wj');
athread.Connection.ReadBuffer(filelen,4);
progressbar1.Max:=filelen;
savestream:=tfilestream.Create(savedialog1.FileName,fmcreate);
listbox1.Items.Add('开始接收');
end;
try
repeat
if filelen-savestream.Position>sizeof(buf) then
ReadCount := SizeOf(Buf)
else
ReadCount :=filelen-savestream.Position;
AThread.Connection.ReadBuffer(Buf, ReadCount);
saveStream.WriteBuffer(Buf, ReadCount);
progressbar1.Position:=savestream.Position;
athread.Connection.WriteLn();
Application.ProcessMessages;
until saveStream.size >= Filelen;
except
xcfilename:=filename;
xcsavestream:=savestream;
xcpos:=savestream.Position;
listbox1.Items.Add('连接中断');
//AThread.Connection.WriteLn('OK');
end
end;
if filename=xcfilename then
begin
athread.Connection.WriteLn('xc');
athread.Connection.ReadBuffer(filelen,4);
progressbar1.Max:=filelen;
athread.Connection.WriteBuffer(xcpos,4);
savestream:=xcsavestream;
savestream.Position:=xcpos;
try
repeat
if filelen-savestream.Position>sizeof(buf) then
ReadCount := SizeOf(Buf)
else
ReadCount :=filelen-savestream.Position;
AThread.Connection.ReadBuffer(Buf, ReadCount);
saveStream.WriteBuffer(Buf, ReadCount);
progressbar1.Position:=savestream.Position;
athread.Connection.WriteLn();
Application.ProcessMessages;
until saveStream.size >= Filelen;
listbox1.Items.Add('传输完成');
xcfilename:='';
except
xcfilename:=filename;
xcsavestream:=savestream;
xcpos:=savestream.Position;
end
end;
end;
savestream.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
button5.Enabled:=false;
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
form2.ShowModal;
end;
end.