刚在书上看到,发都这里开给还不懂的朋友看。
代码是最少滴。
注释也是有滴。嘿嘿
代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdTCPConnection, IdTCPClient, IdBaseComponent, IdComponent,
IdTCPServer, StdCtrls;
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
IdTCPClient1: TIdTCPClient;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Label3: TLabel;
Label4: TLabel;
Button2: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure Button1Click(Sender: TObject);
procedure Label3Click(Sender: TObject);
procedure Label4Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
t:tmemorystream;
begin
t:=tmemorystream.Create;
athread.Connection.ReadStream(t,-1,true);
t.SaveToFile(edit2.text);
if FileExists(edit2.text) then
showmessage('文件发送成功');
t.Free
end;
procedure TForm1.Button1Click(Sender: TObject);
var
tt:tmemorystream;
begin
tt:=tmemorystream.Create;
tt.LoadFromFile(edit1.text);
idtcpclient1.Connect();
idtcpclient1.WriteStream(tt);
idtcpclient1.Disconnect();
tt.free;
end;
procedure TForm1.Label3Click(Sender: TObject);
begin
if OpenDialog1.Execute then
edit1.text:=OpenDialog1.FileName;
end;
procedure TForm1.Label4Click(Sender: TObject);
begin
if SaveDialog1.Execute then
edit2.text:=SaveDialog1.FileName;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
end.