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

相关资料:
http://blog.csdn.net/earbao/article/details/46514313
http://blog.csdn.net/lin_strong/article/details/51592093
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。
实例代码:
//Use IdContext,IdGlobal这两个单元
//不能使用string类型,在分存分配时会出错的。 unit Unit1; interface uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.ScrollBox,
FMX.Memo, FMX.Controls.Presentation, FMX.StdCtrls, IdTCPConnection,
IdTCPClient, IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer,
IdContext, IdIOHandler, Data.DBByteBuffer, IdGlobal, FMX.Edit, FMX.Objects; //常量区
const
ServerPort = ; //基础数据类型 //方案一:手机不支持这种的
//type
// TUser = record
// UserName,UserID:String[20];
// IP:String[16];
// Port:Integer;
// Msg:String[100];
// Arr:Array[1..9] of String[20];
// flag:Boolean;
// Cmd:String[20];
//end; //方案二:手机PC都支持
type
TUser = record
UserName, UserID:String;
IP: String;
Port: Integer;
Msg: String;
Arr: Array[..] of String;
flag: Boolean;
Cmd: String;
end; //方案三:记录指针
TSMSHead = packed record
SequenceID: Integer;
CommandID: Integer;
TotalLength: Integer;
end;
PSMSHead = ^TSMSHead; TSMSConnect = packed record
Head: TSMSHead;
UID: array[..] of char;
PWD: array[..] of char;
Version: Byte;
end;
PSMSConnect = ^TSMSConnect; type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
IdTCPClient1: TIdTCPClient;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Memo1: TMemo;
Label1: TLabel;
Button5: TButton;
Button6: TButton;
Edit1: TEdit;
Label3: TLabel;
Image1: TImage;
procedure Button3Click(Sender: TObject);
procedure IdTCPServer1Connect(AContext: TIdContext);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure IdTCPServer1Execute(AContext: TIdContext);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
FileSize: Int64;//发送文件时用到,保存文件大小。
end; var
Form1: TForm1; implementation {$R *.fmx}
{$R *.SmXhdpiPh.fmx ANDROID}
{$R *.NmXhdpiPh.fmx ANDROID} //连接服务器
procedure TForm1.Button3Click(Sender: TObject);
begin
IdTCPClient1.Host:= Edit1.Text; //服务器的IP
IdTCPClient1.Port:= ServerPort; //服务器的端口
IdTCPClient1.Connect; //连接服务器
//必须连接成功后才可以,放前面会报错
IdTCPClient1.IOHandler.DefStringEncoding := IndyTextEncoding_UTF8(); //中文处理
end; //连接提示
procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
AContext.Connection.IOHandler.DefStringEncoding := IndyTextEncoding_UTF8(); //中文处理
Memo1.Lines.Add('有用户连接');
Memo1.Lines.Add('');
end; //记录类型(TIdBytes)
procedure TForm1.Button4Click(Sender: TObject);
var
User: TUser;
sby: TIdBytes;
begin
with user do
begin
UserName := 'Wyatt';
UserID := '';
Ip:= '192.168.1.188';
Port := ;
Msg := 'ABC';
cmd := 'Quit';
end;
sBy := RawTOBytes(user, sizeof(user));
IdTCPClient1.IOHandler.Write(sBy);
end; //记录类型(TMemoryStream)
procedure TForm1.Button5Click(Sender: TObject);
var
User: TUser;
Mon: TMemoryStream;
begin
with user do
begin
UserName := 'Wyatt';
UserID := '';
Ip := '192.168.1.188';
Port := ;
Msg := '';
cmd := 'Quit';
end;
Mon := TMemoryStream.Create;
try
Mon.WriteBuffer(user, sizeof(user));
IdTCPClient1.IOHandler.Write(Mon);
finally
Mon.Free;
end;
end; //记录指针(TIdBytes)
procedure TForm1.Button1Click(Sender: TObject);
var
pkt: TSMSConnect;
buf: TIdBytes;
begin
// 填写pkt
pkt.Head.SequenceID := ;
pkt.Head.CommandID := ;
StrLCopy(pkt.UID, 'I love you 你好', SizeOf(pkt.UID));
// ...
// 准备缓冲区数据
SetLength(buf, SizeOf(TSMSConnect));
Move(pkt, buf[], SizeOf(TSMSConnect));
IdTCPClient1.Socket.Write(buf);
end; //发送文件(TFileStream)
procedure TForm1.Button2Click(Sender: TObject);
var
oTmpStream: TFileStream;
begin
Try
oTmpStream := TFileStream.Create('E:\123.jpg', fmOpenRead);
oTmpStream.Position := ;
FileSize := oTmpStream.Size;
IdTCPClient1.IOHandler.Write(oTmpStream);
oTmpStream.Free;
except
//
end;
end; //Byte方法(未实现)
procedure TForm1.Button6Click(Sender: TObject);
const
data: array[..] of Byte = (, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , );
var
ReqBuf, RespBuf: TIdBytes;
I: Integer;
begin
SetLength(ReqBuf, );
for I := to Length(ReqBuf) - do
begin
ReqBuf[I] := data[I];
end;
try
IdTCPClient1.IOHandler.Write(ReqBuf);
except
on e: Exception do
begin
ShowMessage('发送' + e.Message);
Exit;
end;
end;
end; //发送字符串(string)
procedure TForm1.Button7Click(Sender: TObject);
begin
//发送文本
IdTCPClient1.IOHandler.writeln(Memo1.Text);
end; procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
//接收string字符串
var
TempStr: string;
begin
//服务器接收
TempStr := AContext.Connection.IOHandler.ReadLn;
Memo1.Lines.Add(TempStr); ////接收文件流
//var
// omStream: TMemoryStream;
//begin
// omStream := TMemoryStream.Create;
// AContext.Connection.IOHandler.ReadStream(omStream, FileSize);
// omStream.SaveToFile('E:\01.jpg');
// omStream.Free; //接收记录类型(TIdBytes)
//var
// user: TUser;
// buf: TidBytes;
//begin
// Acontext.Connection.IOHandler.ReadBytes(buf, sizeof(user));
// BytesToRaw(buf, user, sizeof(user));
// with user, memo1.Lines do
// begin
// Add(userName);
// add(userID);
// add(ip);
// add(inttostr(port));
// add(Msg);
// add(cmd);
// add('');
// end; //接收记录类型(TMemoryStream)
//var
// user: TUser;
// Mon: TMemoryStream;
//begin
// Mon := TMemoryStream.Create;
// try
// AContext.Connection.IOHandler.ReadStream(Mon);
// Mon.ReadBuffer(user, Sizeof(user)); //Indy 10必须将记录类型转换为TidBytes类型才能发送
// with user, memo1.Lines do
// begin
// Add(userName);
// add(userID);
// add(ip);
// add(inttostr(port));
// add(Msg);
// add(cmd);
// add('');
// end;
// finally
// Mon.Free;
// end; //接收记录指针(TIdBytes)
//var
// pkt: PSMSConnect;
// buf: TIdBytes;
//begin
// SetLength(buf, SizeOf(TSMSConnect));
// AContext.Connection.Socket.ReadBytes(buf, SizeOf(TSMSConnect), False);
// pkt := PSMSConnect(@buf[0]);
// Assert(pkt.Head.SequenceID = 1000);
// Memo1.Lines.Add(pkt.UID);
// Memo1.Lines.Add('');
end; end.
Delphi实例-IdTCPServer和IdTCPClient的使用(支持文件发送)的更多相关文章
- (转载)delphi实例TDBGrid用右键菜单复制行粘贴行
delphi实例TDBGrid用右键菜单复制行粘贴行 这个从本质上来说就是DBGrid后台数据库的插入 右键复制当前行的相关数据到临时变量点粘贴时,覆盖数据或插入数据! db为数据库: 字段名id,n ...
- Delphi实例之一个简易的浏览器的实现
Delphi实例之一个简易的浏览器的实现 Delphi7的WebBrowser组件提供了很多不错的网页设计的功能,下面做一个简单的浏览器.组件很简单按照下面摆放就行了. 这是运行后的效果 源代码 主页 ...
- Delphi实例之橡皮筋画图的实现
Delphi实例之橡皮筋画图的实现 在<Delphi7基础教程>这本书的练习中提到过一个橡皮筋画图的例子,书上的源码是错误的!不知道是打印的错误还是本身源码就有问题,我将它改了过来. 在F ...
- Delphi实例之绘制正弦函数图像
Delphi实例之绘制正弦函数图像 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphic ...
- Delphi阿里云邮件推送【支持单一发信、邮件批量发送和获取指定条件下的发送数据】
作者QQ:(648437169) 点击下载➨Delphi阿里云邮件推送 阿里云api文档 [Delphi阿里云邮件推送]支持SingleSendMail(单一发信接口). ...
- Delphi RSA签名与验签【支持SHA1WithRSA(RSA1)、SHA256WithRSA(RSA2)和MD5WithRSA签名与验签】
作者QQ:(648437169) 点击下载➨ RSA签名与验签 [delphi RSA签名与验签]支持3种方式签名与验签(SHA1WithRSA(RSA1).SHA256WithRSA(RSA2)和M ...
- Delphi阿里云对象存储OSS【支持上传文件、下载文件、删除文件、创建目录、删除目录、Bucket操作等】
作者QQ:(648437169) 点击下载➨Delphi阿里云对象存储OSS 阿里云api文档 [Delphi阿里云对象存储OSS]支持 获取Bucket列表.设置Bucket ...
- Delphi中对BCD码的直接支持 (转)
最近在Delphi下写软件,需要将数据转换为BCD码和将BCD码转换为其它数据类型,从网上搜索了一下,没有发现好的函数,于是就想自定义函数来完成BCD与其它格式的数据转换功能.但最终没有动手写,先查查 ...
- Delphi实例之一个较复杂的记事本的实现
http://www.mamicode.com/info-detail-110813.html delphi中控件位置及自动排版的问题 http://blog.csdn.net/avan_lau/ar ...
随机推荐
- (一)CSS三种插入方式
CSS概述 CSS(Cascading Style Sheets)指层叠样式表,样式定义了如何显示HTML元素. 样式通常存储在样式表中,样式与HTML分离解决了内容与表现分离的问题. 多个样式表可以 ...
- PHP Redis 普通封装类
class redisInit { private $redis; //redis对象 /** * 初始化Redis * $config = array( * 'server' => '127. ...
- windows和mac下分别配置虚拟主机
windows下配置 1.找到apache的配置文件,httpd.conf 2.找到 LoadModule rewrite_module modules/mod_rewrite.so 去掉前边的# 3 ...
- Linux 下查看文件字符编码和转换编码
Linux 下查看文件字符编码和转换编码 如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而Linu ...
- 使用ssh公钥密钥自动登陆linux服务器
转自:http://7056824.blog.51cto.com/69854/403669 作为一名 linux 管理员,在多台 Linux 服务器上登陆进行远程操作是每天工作的一部分.但随着服务器的 ...
- sdut 2847 Monitor (思维题)
题目 题意:给定a, b, x, y; 求使c, d; 使c:d = x :y; 且c<=a, d<=b, 而且c, d尽量大. 先求最小倍数, 再用最小倍数乘 x, y; #inclu ...
- HDU 4923
题目大意: 给出一串序列Ai{0,1},求一个序列Bi[0,1](Bi<Bi+1),使得sigama(Ai-Bi)^2最小 思路: 若B相同,则取A的平均数可使方差最小 若B有序, 若A== ...
- bzoj3275: Number
最小割...然后推一下可知不能的情况必定为一奇一偶,于是s->奇->偶->t.跑最小割即可. #include<cstdio> #include<cstring&g ...
- 定义 androidlistview 滚动条位置
1.找到每一页的最后一条数据的位置 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, ...
- SharedPreferencesUtil
用于缓存一个临时的变量 比如 SharedPreferencesUtil.put(getApplicationContext(), "userImage", user.conten ...