unit UClient;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient;
type
TFClient = class(TForm)
ListBox1: TListBox;
TCPClient: TIdTCPClient;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FClient: TFClient;
implementation
{$R *.dfm}
procedure TFClient.Button1Click(Sender: TObject);
begin
with TCPClient do
begin
//建立与服务器的连接
Connect;
try
//将客户端收到的数据添加到列表中
ListBox1.Items.Add(ReadLn);
finally
Disconnect;
end;
end;
end;
end.
unit U_Server;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer;
type
TFServer = class(TForm)
IdTCPServer1: TIdTCPServer;
procedure FormCreate(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FServer: TFServer;
implementation
{$R *.dfm}
procedure TFServer.FormCreate(Sender: TObject);
begin
IdTCPServer1.Active := True;
end;
procedure TFServer.IdTCPServer1Execute(AThread: TIdPeerThread);
begin
with AThread.Connection do
begin
//向与服务器连接的客户发送一条简单的消息
WriteLn('欢迎访问TCP服务器!');
//断开连接
Disconnect;
end;
end;
end.
应用TIdTCPClient和TIdTCPServer组件
最新推荐文章于 2019-08-20 19:59:00 发布
本文介绍了一个简单的TCP客户端与服务器之间的通信示例。客户端通过按钮触发与服务器的连接,并接收服务器的消息;服务器监听客户端连接请求并向其发送欢迎信息。
6393

被折叠的 条评论
为什么被折叠?



