然後是客戶端:
代碼如下:
1.unit Unit1;
2.
3.interface
4.
5.uses
6. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7. Dialogs, IDBaseComponent, IdComponent, IdGlobal, IdTCPConnection, IdTCPClIEnt,
8. ExtCtrls, StdCtrls, ComCtrls, XPMan;
9.
10.type
11. TForm1 = class(TForm)
12. IdTCPClient: TIdTCPClIEnt;
13. btnConnect: TButton;
14. tmrCheckServerMsg: TTimer;
15. btnDisconect: TButton;
16. edtMsg: TEdit;
17. pbProgress: TProgressBar;
18. mmoInfo: TMemo;
19. XPManifest1: TXPManifest;
20. procedure btnConnectClick(Sender: TObject);
21. procedure btnDisconectClick(Sender: TObject);
22. procedure edtMsgKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
23. procedure FormClose(Sender: TObject; var Action: TCloseAction);
24. procedure FormCreate(Sender: TObject);
25. procedure FormShow(Sender: TObject);
26. procedure IdTCPClIEntWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
27. procedure tmrCheckServerMsgTimer(Sender: TObject);
28. private
29. { Private declarations }
30. public
31. { Public declarations }
32. end;
33.
34.var
35. Form1: TForm1;
36.
37.implementation
38.
39.uses TypInfo;
40.
41.{$R *.dfm}
42.
43.procedure TForm1.btnConnectClick(Sender: TObject);
44.var
45. Response: string;
46. UserName: string;
47. HostName: array[0..MAX_COMPUTERNAME_LENGTH] of char;
48. Length: DWord;
49.begin
50. IdTCPClIEnt.ConnectTimeout := 5000;
51. IdTCPClIEnt.Connect;
52. //UserName := Format('U%.5d', [Random(99999)]);
53. Length := SizeOf(HostName);
54. GetComputerName(HostName, Length);
55. UserName := HostName;
56.
57. IdTCPClIEnt.IOHandler.WriteLn(UserName);
58. Response := IdTCPClIEnt.IOHandler.ReadLn;
59. if SameText(Response, 'LOGINED') then
60. begin
61. btnDisconect.Enabled := True;
62. btnConnect.Enabled := False;
63. tmrCheckServerMsg.Enabled := True;
64. Caption := 'ClIEnt - ' + UserName;
65. end
66. else raise Exception.CreateFmt('登錄失敗: "%s"', [Response]);
67.end;
68.
69.procedure TForm1.btnDisconectClick(Sender: TObject);
70.begin
71. btnConnect.Enabled := True;
72. btnDisconect.Enabled := False;
73. tmrCheckServerMsg.Enabled := False;
74. Caption := 'ClIEnt';
75. IdTCPClIEnt.Disconnect;
76.end;
77.
78.procedure TForm1.edtMsgKeyDown(Sender: TObject; var Key: Word; Shift:
79. TShiftState);
80.begin
81. if Key = VK_RETURN then
82. begin
83. if not IdTCPClIEnt.Connected then Exit;
84. if edtMsg.Text <> '' then
85. begin
86. IdTCPClIEnt.IOHandler.WriteLn(edtMsg.Text, enUTF8);
87. mmoInfo.Lines.Add(Format('發送消息: "%s"', [edtMsg.Text]));
88. edtMsg.Clear;
89. end;
90. Key := 0;
91. end;
92.end;
93.
94.procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
95.begin
96. try
97. if IdTCPClIEnt.Connected then
98. btnDisconect.Click;
99. except
100. end;
101.end;
102.
103.procedure TForm1.FormCreate(Sender: TObject);
104.begin
105. Randomize;
106. IdTCPClIEnt.Host := '192.168.2.148';
107. IdTCPClIEnt.Port := 3030;
108.end;
109.
110.procedure TForm1.FormShow(Sender: TObject);
111.begin
112. btnConnect.Click;
113.end;
114.
115.procedure TForm1.IdTCPClIEntWork(ASender: TObject; AWorkMode: TWorkMode;
116. AWorkCount: Int64);
117.begin
118. pbProgress.Position := AWorkCount;
119. Application.ProcessMessages;
120.end;
121.
122.type
123. TSizeType = (stB, stK, stM, stG, stT);
124.
125.function FormatFileSize(Size: Extended; MaxSizeType: TSizeType; var ReturnSizeType: TSizeType;
126. const IncludeComma: Boolean = True): string; overload;
127.const
128. FormatStr: array[Boolean] of string = ('0.##', '#,##0.##'); {do not localize}
129.var
130. DivCount: Integer;
131.begin
132. ReturnSizeType := stB;
133. DivCount := 0;
134. while (Size >= 1024) and (ReturnSizeType <> MaxSizeType) do
135. begin
136. Size := Size / 1024;
137. Inc(DivCount);
138. case DivCount of
139. 1: ReturnSizeType := stK;
140. 2: ReturnSizeType := stM;
141. 3: ReturnSizeType := stG;
142. 4: ReturnSizeType := stT;
143. end;
144. end;
145. Result := FormatFloat(FormatStr[IncludeComma], Size);
146.end;
147.
148.function FormatFileSize(Size: Extended; MaxSizeType: TSizeType;
149. const IncludeComma: Boolean = True): string; overload;
150.resourcestring
151. RSC_BYTE = '字節';
152.var
153. ReturnSt: TSizeType;
154.begin
155. Result := FormatFileSize(Size, stT, ReturnSt, True) + ' ' +
156. Copy(GetEnumName(TypeInfo(TSizeType), Ord(ReturnSt)), 3, 1);
157. if ReturnSt = stB then
158. begin
159. Delete(Result, Length(Result), 1);
160. Result := Result + RSC_BYTE;
161. end
162. else
163. Result := Result + 'B'; {do not localize}
164.end;
165.
166.procedure TForm1.tmrCheckServerMsgTimer(Sender: TObject);
167.var
168. CmdStr: string;
169. FSize: Int64;
170. FStream: TFileStream;
171. SaveFileName: string;
172.begin
173. CmdStr := '';
174. if IdTCPClIEnt.Connected then
175. begin
176. IdTCPClIEnt.IOHandler.CheckForDataOnSource(250);
177. if not IdTCPClIEnt.IOHandler.InputBufferIsEmpty then
178. begin
179. tmrCheckServerMsg.Enabled := False;
180. try
181. CmdStr := IdTCPClIEnt.IOHandler.ReadLn(enUTF8);
182. CmdStr := System.UTF8Encode(CmdStr);
183. if SameText(Copy(CmdStr, 1, 4), 'FILE') then
184. begin
185. SaveFileName := Trim(Copy(CmdStr, 5, Length(CmdStr)));
186. mmoInfo.Lines.Add('准備接收文件....');
187. IdTCPClIEnt.IOHandler.WriteLn('SIZE');
188. FSize :=IdTCPClIEnt.IOHandler.ReadInt64(False);
189. if FSize > 0 then
190. begin
191. pbProgress.Max := FSize;
192. pbProgress.Position := 0;
193. mmoInfo.Lines.Add('文件大小 =' + FormatFileSize(FSize, stK) + '; 正在接收中...');
194. IdTCPClIEnt.IOHandler.WriteLn('READY');
195. while True do196. begin
197. if FileExists(ExtractFilePath(ParamStr(0)) + SaveFileName) then
198. SaveFileName := '~' + SaveFileName
199. else Break;
200. end;
201. FStream := TFileStream.Create(ExtractFilePath(ParamStr(0))
202. + SaveFileName,
203. fmCreate);
204. try
205. IdTCPClIEnt.IOHandler.LargeStream := True;
206. IdTCPClIEnt.IOHandler.ReadStream(FStream, FSize);
207. IdTCPClIEnt.IOHandler.LargeStream := False;
208. IdTCPClIEnt.IOHandler.WriteLn('OK');
209. if IdTCPClIEnt.IOHandler.ReadLn = 'DONE' then
210. mmoInfo.Lines.Add('接收成功!')
211. finally
212. FStream.Free;
213. end;
214. end
215. else begin
216. mmoInfo.Lines.Add('接收失敗!');
217. IdTCPClIEnt.IOHandler.WriteLn('CANCEL');
218. end;
219. end
220. else
221. mmoInfo.Lines.Add('接收文本信息: ' + CmdStr)
222. finally
223. tmrCheckServerMsg.Enabled := True;
224. end;
225. end;
226. end;
227.end;
228.
229.end.