// 服务端default port 8080
// 客户端发送port 8080
//注意你定的结构时,你的string类型为定长,
//共同学习中......
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPServer,
IdTCPConnection, IdTCPClient, ExtCtrls, ComCtrls;
type
TfrmClient = class(TForm)
Label1: TLabel;
edtFileName: TEdit;
btnBrowse: TButton;
btnSend: TButton;
cmbIP: TComboBox;
Label2: TLabel;
cmbPort: TComboBox;
Label3: TLabel;
OpenDialog1: TOpenDialog;
IdTCPServer1: TIdTCPServer;
IdTCPClient1: TIdTCPClient;
Bevel1: TBevel;
procedure btnBrowseClick(Sender: TObject);
procedure btnSendClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
TPerson = record
Name: string[20];
Age: Integer;
end;
var
frmClient: TfrmClient;
implementation
{$R *.dfm}
procedure TfrmClient.btnBrowseClick(Sender: TObject);
begin
if OpenDialog1.Execute then
edtFilename.Text := OpenDialog1.FileName;
end;
procedure TfrmClient.btnSendClick(Sender: TObject);
var
ms,mstmp: TMemoryStream;
per1: TPerson;
begin
IdTCPClient1.Host := cmbIP.Text;
IdTCPClient1.Port := StrToInt(cmbPort.Text);
per1.Name := 'DelphiBBs';
per1.Age := 5;
ms := TMemoryStream.Create;
mstmp := TMemoryStream.Create;
// ms.ReadBuffer(per1,SizeOf(per1));
ms.Write(per1,SizeOf(per1)) ;
mstmp.LoadFromFile(OpenDialog1.FileName);
ms.CopyFrom(mstmp,mstmp.Size);
IdTCPClient1.Connect();
IdTCPClient1.WriteStream(ms);
IdTCPClient1.Disconnect();
ms.Free;
mstmp.Free;
end;
procedure TfrmClient.FormCreate(Sender: TObject);
begin
IdTCPServer1.Active := True;
end;
procedure TfrmClient.IdTCPServer1Execute(AThread: TIdPeerThread);
var
ms: TMemoryStream;
mstmp :TMemoryStream;
per2: TPerson;
begin
ms := TMemoryStream.Create;
mstmp := TMemoryStream.Create;
AThread.Connection.ReadStream(ms,-1,True);
ms.Position := 0;
ms.Read(per2,SizeOf(per2));
mstmp.CopyFrom(ms,ms.Size-Sizeof(per2));
mstmp.SaveToFile('D:/DelphiDemo/Socket/Tcp/tt.txt');
OutputDebugString('Save Successful!');
ms.Free;
mstmp.Free;
// AThread.Connection.Disconnect;
end;
end.