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