码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi中Indy10的IdTcpClient和IdTcpServer数据通信

时间:2021-05-24 08:08:12      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:cap   client   tom   socket   nal   false   read   sch   --   

---------例子是XE的----

只是本人测试的,仅供学习!代码很乱!

 

在一个程序中测试 IdTcpClient和IdTcpServer有点坑,

测试的时候最好写Client和Server两个程序测试

 

--------------------

 

-------------------------Unit开始

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdContext, StdCtrls, IdTCPConnection, IdTCPClient, IdBaseComponent,
IdComponent, IdCustomTCPServer, IdTCPServer,IdGlobal, IdIOHandler,
IdIOHandlerStream, IdIntercept, IdIOHandlerSocket, IdIOHandlerStack, ExtCtrls,
IdAntiFreezeBase, IdAntiFreeze ;

type
TMData = record
id:Integer;
Name:Array[0..20] of Char;
Age:Byte;
UpdateTime:double;
end;
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
IdTCPClient1: TIdTCPClient;
Button4: TButton;
Button5: TButton;
Timer1: TTimer;
Edit1: TEdit;
Button6: TButton;
IdAntiFreeze1: TIdAntiFreeze;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure IdTCPServer1Execute(AContext: TIdContext);
procedure FormShow(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure IdTCPServer1Connect(AContext: TIdContext);
procedure Button5Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure IdTCPClient1Connected(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
ClientIPList:TList;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses IdSchedulerOfThread;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
SendD: TMData;
begin
if not IdTCPClient1.Connected then
IdTCPClient1.Connect;
SendD.ID := 10;
StrPCopy(SendD.Name,‘Delphi 您好‘);
SendD.age := 18;
SendD.UpdateTime := Now;
IdTCPClient1.IOHandler.DefStringEncoding:=TEncoding.ASCII;//TEncoding.ASCII;//TEncoding.Unicode;
IdTCPClient1.IOHandler.Write(#100); //提前发送一个标识符,用于区分数据
IdTCPClient1.IOHandler.Write(RawToBytes(SendD, SizeOf(SendD)));

end;

procedure TForm1.Button4Click(Sender: TObject);
var
MyIdTCPConnection:TIdTCPConnection;
vIOHandler:TIdIOHandler;
vv:TIdContext;
begin
try
//MyIdTCPConnection:=TIdTCPConnection.Create(nil);
//vIOHandler:=TIdIOHandler.Create(nil);
//MyIdTCPConnection.IOHandler :=vIOHandler;
//
//ShowMessage(TIdContext(ClientIPList.Items[0]).Connection.Name+‘,‘+TIdContext(ClientIPList.Items[0]).binding.peerIp);

//ShowMessage(IntToStr(TIdTCPConnection(ClientIPList.Items[0]).IOHandler.Port)+‘,,‘+TIdTCPConnection(ClientIPList.Items[0]).IOHandler.Host);
//MyIdTCPConnection.IOHandler.Port:=5050;
//MyIdTCPConnection.IOHandler.Host:=‘192.168.1.50‘;
//MyIdTCPConnection.IOHandler.DefStringEncoding:=TEncoding.ASCII;//TEncoding.ASCII;//TEncoding.Unicode;
//MyIdTCPConnection.Socket.Binding.SetPeer(‘192.168.1.50‘,5050);
//MyIdTCPConnection.Socket.Open ;
//MyIdTCPConnection.IOHandler.Open;
//MyIdTCPConnection.IOHandler.Write(‘Qaaam‘);
//ShowMessage(TIdTCPConnection(ClientIPList.Items[0]));
//TIdContext(ClientIPList.Items[0]).Connection.IOHandler.Write(‘AAAA‘);
//TIdTCPConnection(ClientIPList.Items[0]).IOHandler.Write(#10,TIdTextEncoding.ASCII);
//TIdTCPConnection(ClientIPList.Items[0]).IOHandler.Writeln(‘QAAAAN‘,TIdTextEncoding.ASCII);
//----------------------------
//ShowMessage(IntToStr(TIdYarnOfThread(TIdContext(ClientIPList.Items[0]).Yarn).Thread.ThreadID));
//if TIdYarnOfThread(TIdContext(ClientIPList.Items[0]).Yarn).Thread.Suspended then
//TIdYarnOfThread(TIdContext(ClientIPList.Items[0]).Yarn).Thread.Resume;
////vv:=TIdContext(ClientIPList.Items[0]);
//TIdContext(ClientIPList.Items[0]).Connection.IOHandler.CheckForDisconnect(True, True);
////vv.Connection.IOHandler.DefStringEncoding := TIdTextEncoding.ASCII;
//TIdContext(ClientIPList.Items[0]).Connection.IOHandler.Write(‘QAAAAN‘,TIdTextEncoding.ASCII);
////if vv.Connection.IOHandler.Opened then
////ShowMessage(‘已连接‘);
////vv.Connection.Socket.Write(‘200: 数据接收成功!‘);
//啊啊啊啊啊啊啊啊,气死人了!测试了这么多,以为代码有问题!!!!在同一个进程中这玩意有问题,
//建立客户端服务器两个程序的时候,这样的代码可以!妈耶!不知道什么情况!!!
vv:=TIdContext(IdTCPServer1.Contexts.LockList.Items[0]);
vv.Connection.IOHandler.Write(‘QAAAAN‘);

//------------------------------
//TIdTCPConnection(ClientIPList.Items[0]).SendCmd(‘888‘) ;
finally
//FreeAndNil(MyIdTCPConnection);
//FreeAndNil(vIOHandler);
IdTCPServer1.Contexts.UnlockList;
end;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
Timer1.Enabled:=not Timer1.Enabled ;
if Timer1.Enabled then
Button5.Caption:=‘不显示‘
else
Button5.Caption:=‘显示‘;
//ShowMessage(IdTCPClient1.Intercept.ReadString(IdTCPClient1.IOHandler.InputBuffer.Size,TIdTextEncoding.UTF8 ));
//ShowMessage(IdTCPClient1.IOHandler.ReadString(IdTCPClient1.IOHandler.InputBuffer.Size,) );
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
//IdTCPClient1.IOHandler.Write(#10);
if not IdTCPClient1.Connected then
IdTCPClient1.Connect;
IdTCPClient1.IOHandler.DefStringEncoding:=TEncoding.ASCII;//TEncoding.ASCII;//TEncoding.Unicode;
//IdTCPClient1.IOHandler.Write(#0); //提前发送一个标识符,用于区分数据
IdTCPClient1.IOHandler.Write(‘BAAAAC‘,TIdTextEncoding.ASCII);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(ClientIPList);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
ClientIPList:=TList.Create;
IdTCPServer1.Active:=True;

end;

procedure TForm1.IdTCPClient1Connected(Sender: TObject);
begin
//ShowMessage(‘连接客户端‘);
end;

procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin

if Length(AContext.Binding.PeerIP)>5 then//AContext.Connection.Socket.Binding.PeerIP;
ClientIPList.Add(AContext);
//TIdYarnOfThread(acontext.Yarn).Thread.ThreadID;

end;

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
RData:TMData;
buf:TIdBytes;
sCmd:Char;
sList:TStrings;
I:Integer;
ListCount:Integer;
ss:string;
begin
//ACONTEXT.Connection.IOHandler.WriteLn(‘Recive Message!‘);
//AContext.Connection
{sCmd := AContext.Connection.IOHandler.ReadChar; //先读取Char结构数据
if sCmd = #100 then //接收结构体
begin
AContext.Connection.IOHandler.ReadBytes(buf,SizeOf(RData));
BytesToRaw(buf, RData, SizeOf(RData));
with Memo1.lines do begin
Add(‘IP:‘+AContext.Connection.Socket.Binding.PeerIP);
Add(‘ID:‘+Inttostr(RData.Id));
Add(‘Name:‘+StrPas(RData.Name));
Add(‘Age:‘+Inttostr(RData.age));
Add(‘UpdateTime:‘+DateTimeToStr(RData.UpdateTime));
end;
end //TCPClient.IOHandler.DefStringEncoding:=TEncoding.ASCII;
else }
//sCmd := AContext.Connection.IOHandler.ReadChar ;
ss:=AContext.Connection.IOHandler.Readstring(AContext.Connection.IOHandler.InputBuffer.Size,TIdTextEncoding.ASCII );
if ss<>‘‘ then
begin
Memo1.Lines.Add(ss); //AContext.Connection.IOHandler.InputBuffer.Size,
AContext.Connection.IOHandler.Write(‘88888888888‘,TIdTextEncoding.ASCII );
end;


end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
ss:string;
begin
ss:=IdTCPClient1.IOHandler.ReadString(IdTCPClient1.IOHandler.InputBuffer.Size);
Edit2.Text:=ss;
if ss<>‘‘ then
Edit1.Text:=ss


//IdTCPClient1.IOHandler.InputBuffer.Clear;
end;

end.

--------------------------------------
Delphi XE IdTCPClient1 和 IdTCPServer1 数据的发送与接收(indy10)_136.la https://www.136.la/shida/show-131002.html
-------------------------------------------------------------------------------------

1、IdTCPClient1 端 发送数据

1.1 发送结构体:

//定义结构体
TMData = record
id:Integer;
Name:Array[0..20] of Char;
Age:Byte;
UpdateTime:double;
end;

//发送
procedure TForm2.Button2Click(Sender: TObject);
var
SendD: TMData;
begin
SendD.ID := 10;
StrPCopy(SendD.Name, ‘Delphi 您好‘);
SendData.age := 18;
SendD.UpdateTime := Now;
IdTCPClient1.IOHandler.Write(#100); //提前发送一个标识符,用于区分数据
IdTCPClient1.IOHandler.Write(RawToBytes(SendD, SizeOf(SendD)));
end;
1.2 发送TStrings类型

procedure TForm2.Button3Click(Sender: TObject);
var
sList:TStrings;
I:Integer;
begin
sList := TStringList.Create;
for I :=0 to 50 do
begin
sList.Add(‘数据Test‘ + IntToStr(i));
end;
IdTCPClient1.IOHandler.Write(#200);  
IdTCPClient1.IOHandler.Write(sList.Count);
IdTCPClient1.IOHandler.Write(ToBytes(sList.Text,TIdTextEncoding.UTF8));
end;
1.3 发送一行字符串数据

procedure TForm2.Button4Click(Sender: TObject);
begin
IdTCPClient1.IOHandler.Write(#10);
IdTCPClient1.IOHandler.Write(‘Delphi测试‘,TIdTextEncoding.UTF8);
end;
  

2、IdTCPServer端 接收数据:

procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
RData:TMData;
buf:TIdBytes;
sCmd:Char;
sList:TStrings;
I:Integer;
ListCount:Integer;
begin
sCmd := AContext.Connection.IOHandler.ReadChar; //先读取Char结构数据
if sCmd = #100 then //接收结构体
begin
AContext.Connection.IOHandler.ReadBytes(buf,SizeOf(RData));
BytesToRaw(buf, RData, SizeOf(RData));
with Memo1.lines do begin
Add(‘ID:‘+Inttostr(RData.Id));
Add(‘Name:‘+StrPas(RData.Name));
Add(‘Age:‘+Inttostr(ReadData.age));
Add(‘UpdateTime:‘+DateTimeToStr(RData.UpdateTime));
end;
end else if sCmd = #200 then //接收 TStrings
begin
ListCount := AContext.Connection.IOHandler.ReadLongInt; //ReadLongInt
sList := TStringList.Create;
try
AContext.Connection.IOHandler.ReadStrings(sList,ListCount,TIdTextEncoding.UTF8);
for I :=0 to sList.Count-1 do begin
Memo1.Lines.Add(sList.Strings[I]);
end;
finally
sList.Free;
end;
end else if sCmd = #10 then
begin
Memo1.Lines.Add(AContext.Connection.IOHandler.ReadString(AContext.Connection.IOHandler.InputBuffer.Size,TIdTextEncoding.UTF8) );
end else
AContext.Connection.IOHandler.InputBuffer.Clear;  //清除
end;

 

--------------------------Unit结束

 

----------------Form开始

object Form1: TForm1
Left = 0
Top = 0
Caption = ‘Form1‘
ClientHeight = 383
ClientWidth = 870
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = ‘Tahoma‘
Font.Style = []
OldCreateOrder = False
OnDestroy = FormDestroy
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 32
Top = 272
Width = 75
Height = 25
Caption = ‘Button1‘
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 184
Top = 296
Width = 75
Height = 25
Caption = ‘Button2‘
TabOrder = 1
end
object Button3: TButton
Left = 312
Top = 296
Width = 75
Height = 25
Caption = ‘Button3‘
TabOrder = 2
end
object Memo1: TMemo
Left = 536
Top = 8
Width = 313
Height = 313
ImeName = #20013#25991‘(‘#31616#20307‘) - ‘#25628#29399#25340#38899#36755#20837#27861
Lines.Strings = (
‘Memo1‘)
TabOrder = 3
end
object Button4: TButton
Left = 536
Top = 350
Width = 75
Height = 25
Caption = ‘Button4‘
TabOrder = 4
OnClick = Button4Click
end
object Button5: TButton
Left = 288
Top = 350
Width = 75
Height = 25
Caption = ‘Button5‘
TabOrder = 5
OnClick = Button5Click
end
object Edit1: TEdit
Left = 184
Top = 328
Width = 321
Height = 21
ImeName = #20013#25991‘(‘#31616#20307‘) - ‘#25628#29399#25340#38899#36755#20837#27861
TabOrder = 6
Text = ‘Edit1‘
end
object Button6: TButton
Left = 32
Top = 304
Width = 75
Height = 25
Caption = ‘Button6‘
TabOrder = 7
OnClick = Button6Click
end
object Edit2: TEdit
Left = 369
Top = 354
Width = 96
Height = 21
ImeName = #20013#25991‘(‘#31616#20307‘) - ‘#25628#29399#25340#38899#36755#20837#27861
TabOrder = 8
Text = ‘Edit1‘
end
object IdTCPServer1: TIdTCPServer
Bindings = <
item
IP = ‘192.168.1.50‘
Port = 5050
end>
DefaultPort = 0
OnConnect = IdTCPServer1Connect
OnExecute = IdTCPServer1Execute
Left = 288
Top = 48
end
object IdTCPClient1: TIdTCPClient
OnConnected = IdTCPClient1Connected
ConnectTimeout = 1000
Host = ‘192.168.1.50‘
IPVersion = Id_IPv4
Port = 5050
ReadTimeout = 2000
Left = 104
Top = 40
end
object Timer1: TTimer
Enabled = False
Interval = 100
OnTimer = Timer1Timer
Left = 248
Top = 352
end
object IdAntiFreeze1: TIdAntiFreeze
Left = 88
Top = 104
end
end

 

------------------Form结束

Delphi中Indy10的IdTcpClient和IdTcpServer数据通信

标签:cap   client   tom   socket   nal   false   read   sch   --   

原文地址:https://www.cnblogs.com/dmqhjp/p/14765447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!