Delphi实例-IdTCPServer和IdTCPClient的使用(支持文件发送)

Delphi实例-IdTCPServer和IdTCPClient的使用(支持文件发送)_编程

 

相关资料:

 

http://blog.sina.com.cn/s/blog_44fa172f0102uxs8.html  //indy10的idtcpserver 获取对方IP、端口和本机线程ID 

 

 

结果注意:

1.Use IdContext、IdGlobal 这两个单元
2.不能使用string类型,在分存分配时会出错的。
3.手机中不支持string[20]。
4.中文会乱码,必须设置IndyTextEncoding_UTF8。

 

实例代码: 

 

Delphi实例-IdTCPServer和IdTCPClient的使用(支持文件发送)_编程_02Delphi实例-IdTCPServer和IdTCPClient的使用(支持文件发送)_编程_03
  1 //Use IdContext,IdGlobal这两个单元
  2 //不能使用string类型,在分存分配时会出错的。
  3 
  4 unit Unit1;
  5 
  6 interface
  7 
  8 uses
  9   System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
 10   FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.ScrollBox,
 11   FMX.Memo, FMX.Controls.Presentation, FMX.StdCtrls, IdTCPConnection,
 12   IdTCPClient, IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer,
 13   IdContext, IdIOHandler, Data.DBByteBuffer, IdGlobal, FMX.Edit, FMX.Objects;
 14 
 15 //常量区
 16 const
 17   ServerPort = 1239;
 18 
 19 //基础数据类型
 20 
 21 //方案一:手机不支持这种的
 22 //type
 23 //  TUser = record
 24 //    UserName,UserID:String[20];
 25 //    IP:String[16];
 26 //    Port:Integer;
 27 //    Msg:String[100];
 28 //    Arr:Array[1..9] of String[20];
 29 //    flag:Boolean;
 30 //    Cmd:String[20];
 31 //end;
 32 
 33 //方案二:手机PC都支持
 34 type
 35   TUser = record
 36     UserName, UserID:String;
 37     IP: String;
 38     Port: Integer;
 39     Msg: String;
 40     Arr: Array[1..9] of String;
 41     flag: Boolean;
 42     Cmd: String;
 43 end;
 44 
 45 
 46 //方案三:记录指针
 47 TSMSHead = packed record
 48   SequenceID: Integer;
 49   CommandID: Integer;
 50   TotalLength: Integer;
 51 end;
 52 PSMSHead = ^TSMSHead;
 53 
 54 TSMSConnect = packed record
 55   Head: TSMSHead;
 56   UID: array[0..19] of char;
 57   PWD: array[0..19] of char;
 58   Version: Byte;
 59 end;
 60 PSMSConnect = ^TSMSConnect;
 61 
 62 
 63 
 64 type
 65   TForm1 = class(TForm)
 66     IdTCPServer1: TIdTCPServer;
 67     IdTCPClient1: TIdTCPClient;
 68     Button1: TButton;
 69     Button2: TButton;
 70     Button3: TButton;
 71     Button4: TButton;
 72     Memo1: TMemo;
 73     Label1: TLabel;
 74     Button5: TButton;
 75     Button6: TButton;
 76     Edit1: TEdit;
 77     Label3: TLabel;
 78     Image1: TImage;
 79     procedure Button3Click(Sender: TObject);
 80     procedure IdTCPServer1Connect(AContext: TIdContext);
 81     procedure Button4Click(Sender: TObject);
 82     procedure Button5Click(Sender: TObject);
 83     procedure Button6Click(Sender: TObject);
 84     procedure IdTCPServer1Execute(AContext: TIdContext);
 85     procedure Button1Click(Sender: TObject);
 86     procedure Button2Click(Sender: TObject);
 87   private
 88     { Private declarations }
 89   public
 90     { Public declarations }
 91     FileSize: Int64;//发送文件时用到,保存文件大小。
 92   end;
 93 
 94 var
 95   Form1: TForm1;
 96 
 97 implementation
 98 
 99 {$R *.fmx}
100 {$R *.SmXhdpiPh.fmx ANDROID}
101 {$R *.NmXhdpiPh.fmx ANDROID}
102 
103 //连接服务器
104 procedure TForm1.Button3Click(Sender: TObject);
105 begin
106   IdTCPClient1.Host:= Edit1.Text; //服务器的IP
107   IdTCPClient1.Port:= ServerPort; //服务器的端口
108   IdTCPClient1.Connect; //连接服务器
109   //必须连接成功后才可以,放前面会报错
110   IdTCPClient1.IOHandler.DefStringEncoding := IndyTextEncoding_UTF8(); //中文处理
111 end;
112 
113 //连接提示
114 procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
115 begin
116   AContext.Connection.IOHandler.DefStringEncoding := IndyTextEncoding_UTF8(); //中文处理
117   Memo1.Lines.Add('有用户连接');
118   Memo1.Lines.Add('');
119 end;
120 
121 //记录类型(TIdBytes)
122 procedure TForm1.Button4Click(Sender: TObject);
123 var
124   User: TUser;
125   sby: TIdBytes;
126 begin
127   with user do
128   begin
129     UserName := 'Wyatt';
130     UserID := '7895421';
131     Ip:= '192.168.1.188';
132     Port := 9999;
133     Msg := 'ABC';
134     cmd := 'Quit';
135   end;
136   sBy := RawTOBytes(user, sizeof(user));
137   IdTCPClient1.IOHandler.Write(sBy);
138 end;
139 
140 //记录类型(TMemoryStream)
141 procedure TForm1.Button5Click(Sender: TObject);
142 var
143   User: TUser;
144   Mon: TMemoryStream;
145 begin
146   with user do
147   begin
148     UserName := 'Wyatt';
149     UserID := '85171659';
150     Ip := '192.168.1.188';
151     Port := 9999;
152     Msg := '4567';
153     cmd := 'Quit';
154   end;
155   Mon := TMemoryStream.Create;
156   try
157     Mon.WriteBuffer(user, sizeof(user));
158     IdTCPClient1.IOHandler.Write(Mon);
159   finally
160     Mon.Free;
161   end;
162 end;
163 
164 //记录指针(TIdBytes)
165 procedure TForm1.Button1Click(Sender: TObject);
166 var
167   pkt: TSMSConnect;
168   buf: TIdBytes;
169 begin
170   // 填写pkt
171   pkt.Head.SequenceID := 1000;
172   pkt.Head.CommandID := 119;
173   StrLCopy(pkt.UID, 'I love you 你好', SizeOf(pkt.UID));
174   // ...
175   // 准备缓冲区数据
176   SetLength(buf, SizeOf(TSMSConnect));
177   Move(pkt, buf[0], SizeOf(TSMSConnect));
178   IdTCPClient1.Socket.Write(buf);
179 end;
180 
181 //发送文件(TFileStream)
182 procedure TForm1.Button2Click(Sender: TObject);
183 var
184   oTmpStream: TFileStream;
185 begin
186   Try
187     oTmpStream := TFileStream.Create('E:\123.jpg', fmOpenRead);
188     oTmpStream.Position := 0;
189     FileSize := oTmpStream.Size;
190     IdTCPClient1.IOHandler.Write(oTmpStream);
191     oTmpStream.Free;
192   except
193     //
194   end;
195 end;
196 
197 //Byte方法(未实现)
198 procedure TForm1.Button6Click(Sender: TObject);
199 const
200   data: array[0..32] of Byte = (23, 32, 3, 1, 23, 25, 0, 8, 0, 86, 49, 46, 48, 46, 48, 46, 48, 4, 0, 57, 48, 50, 53, 5, 0, 77, 65, 67, 80, 67, 1, 12, 15);
201 var
202   ReqBuf, RespBuf: TIdBytes;
203   I: Integer;
204 begin
205   SetLength(ReqBuf, 33);
206   for I := 0 to Length(ReqBuf) - 1 do
207   begin
208     ReqBuf[I] := data[I];
209   end;
210   try
211     IdTCPClient1.IOHandler.Write(ReqBuf);
212   except
213     on e: Exception do
214     begin
215       ShowMessage('发送' + e.Message);
216       Exit;
217     end;
218   end;
219 end;
220 
221 //发送字符串(string)
222 procedure TForm1.Button7Click(Sender: TObject);
223 begin
224 //发送文本
225   IdTCPClient1.IOHandler.writeln(Memo1.Text);
226 end;
227 
228 procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
229 //接收string字符串
230 var
231   TempStr: string;
232 begin
233   //服务器接收
234   TempStr := AContext.Connection.IOHandler.ReadLn;
235   Memo1.Lines.Add(TempStr);
236 
237 ////接收文件流
238 //var
239 //  omStream: TMemoryStream;
240 //begin
241 //  omStream := TMemoryStream.Create;
242 //  AContext.Connection.IOHandler.ReadStream(omStream, FileSize);
243 //  omStream.SaveToFile('E:\01.jpg');
244 //  omStream.Free;
245 
246 //接收记录类型(TIdBytes)
247 //var
248 //  user: TUser;
249 //  buf: TidBytes;
250 //begin
251 //  Acontext.Connection.IOHandler.ReadBytes(buf, sizeof(user));
252 //  BytesToRaw(buf, user, sizeof(user));
253 //  with user, memo1.Lines do
254 //  begin
255 //    Add(userName);
256 //    add(userID);
257 //    add(ip);
258 //    add(inttostr(port));
259 //    add(Msg);
260 //    add(cmd);
261 //    add('');
262 //  end;
263 
264 //接收记录类型(TMemoryStream)
265 //var
266 //  user: TUser;
267 //  Mon: TMemoryStream;
268 //begin
269 //  Mon := TMemoryStream.Create;
270 //  try
271 //    AContext.Connection.IOHandler.ReadStream(Mon);
272 //    Mon.ReadBuffer(user, Sizeof(user)); //Indy 10必须将记录类型转换为TidBytes类型才能发送
273 //    with user, memo1.Lines do
274 //    begin
275 //      Add(userName);
276 //      add(userID);
277 //      add(ip);
278 //      add(inttostr(port));
279 //      add(Msg);
280 //      add(cmd);
281 //      add('');
282 //    end;
283 //  finally
284 //    Mon.Free;
285 //  end;
286 
287 //接收记录指针(TIdBytes)
288 //var
289 //  pkt: PSMSConnect;
290 //  buf: TIdBytes;
291 //begin
292 //  SetLength(buf, SizeOf(TSMSConnect));
293 //  AContext.Connection.Socket.ReadBytes(buf, SizeOf(TSMSConnect), False);
294 //  pkt := PSMSConnect(@buf[0]);
295 //  Assert(pkt.Head.SequenceID = 1000);
296 //  Memo1.Lines.Add(pkt.UID);
297 //  Memo1.Lines.Add('');
298 end;
299 
300 end.
View Code