如何设置TIdTcpClient和TIdTcpServer之间的简单连接来发送客户端到服务器的string?【JAVA教程】

!
也想出现在这里? 联系我们
信息

如何设置TIdTcpClient和TIdTcpServer之间的简单连接来发送客户端到服务器的string?,第1张

概述如何设置TIdTcpClient和TIdTcpServer之间的简单连接发送客户端到服务器的string?

我正在使用Delphi xe6来推动一个简单的客户端/服务器连接。 客户表单应该有一个TEdit组件,并且应该将Edit.textstring发送到服务器备忘录。 我想使用Indy组件:TIDTcpserver和TIDTcpClIEnt,但我不知道如何设置客户端和服务器之间的简单连接。

我将不胜感激您的帮助。

windows快捷方式是否支持非常长的参数长度?

在C ++ Builder中使用JavaScript从网页获取variables

Delphi程序和windows 64位兼容性问题

如何检查windows防火墙中的规则?

windows API:等到所有键盘按键全系统释放

服务器:

在Create,Initialize函数中的任何地方:

FIndySrv:=TIDTcpserver.Create(nil); FIndySrv.DefaultPort:=50000; FIndySrv.OnExecute:=DoOnIndyExecute; FIndySrv.Active:=true;

OnExecute:

procedure TForm1.DoOnIndyExecute(AContext: TIDContext); var recv:string; begin recv := AContext.Connection.socket.ReadLn; // … end;

客户:

FIndyClIEnt:=TIDTCPClIEnt.Create(nil); FIndyClIEnt.Host:=\’localhost\’; FIndyClIEnt.Port:=50000; FIndyClIEnt.Connect; FIndyClIEnt.socket.WriteLn(\’Hallo Welt!\’);

由于这个问题是特别问如何从一个VCL组件发送到另一个VCL组件,很可能这个问题将被再次询问/搜索。

使用IDTCPClIEnt尽可能简单。

您只需分配主机和端口,打开连接并将内容写入IDTCPClIEnt的套接字。

由于您必须能够在服务器端读取数据,并且不需要协议(例如,以整数形式发送内容的长度以使服务器知道需要读取多少内容),最简单的方法是使用Socket.WriteLn可以在服务器端使用Socket.ReadLn 。

请注意, OnExecute事件是在自己的IDTcpserver中运行的,所以您将不得不将您的调用同步到mainthread。

可能的方法之一是使用TIDSync的后代。

uses IDSync; type TMySync = Class(TIDSync) private FContent: String; FDestination: TStrings; Constructor Create(const Content: String; Destination: TStrings); overload; protected Procedure DoSynchronize; overrIDe; public End; constructor TMySync.Create(const Content: String; Destination: TStrings); begin inherited Create; FContent := Content; FDestination := Destination; end; procedure TMySync.DoSynchronize; begin inherited; FDestination.Add(FContent); end; procedure TaForm.button1Click(Sender: TObject); begin if not IDTCPClIEnt.Connected then IDTCPClIEnt.Connect; IDTCPClIEnt.socket.WriteLn(Edit.Text); end; procedure TaForm.FormCreate(Sender: TObject); const // the port which will be used for server and ClIEnt C_PORT = 12345; begin IDTcpserver.DefaultPort := C_PORT; IDTcpserver.Active := true; IDTCPClIEnt.Host := \’127.0.0.1\’; IDTCPClIEnt.Port := C_PORT; end; procedure TaForm.IDTcpserverExecute(AContext: TIDContext); begin With TMySync.Create(AContext.Connection.socket.ReadLn,Memo.lines) do begin Synchronize; Free; end; end;

总结

以上是内存溢出为你收集整理的如何设置TIdTcpClient和TIdTcpServer之间的简单连接来发送客户端到服务器的string?全部内容,希望文章能够帮你解决如何设置TIdTcpClient和TIdTcpServer之间的简单连接来发送客户端到服务器的string?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

© 版权声明
THE END
喜欢就支持一下吧
点赞157 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容