新建一个空白窗体:

用RealThinClient组件实现Tcp服务器(1)

加入一个TRtcTcpServer组件,我们来创建一个tcp服务器

用RealThinClient组件实现Tcp服务器(1)

 

如图:改名为TcpServer

用RealThinClient组件实现Tcp服务器(1)

选中TcpServer ,属性作如下配置:修改服务器地址和端口

用RealThinClient组件实现Tcp服务器(1) 

添加两个按钮

用RealThinClient组件实现Tcp服务器(1)

分别命名为:btnListen和btnStopListen

我们要启动服务:

btnListen按钮的代码:

procedure TForm2.btnListenClick(Sender: TObject);
begin
TcpServer.Listen;

end;

 btnListen按钮(停止服务)的代码:

procedure TForm2.btnStopListenClick(Sender: TObject);
begin
 TcpServer.StopListen;
end;

点击监听按钮或者停止监听按钮后,按钮的状态要发生变化,我们在哪里实现比较好呢?

在TcpServer组件的事件里面:

用RealThinClient组件实现Tcp服务器(1)

当开始监听事件触发时,改变按钮的状态:

procedure TForm2.TcpServerListenStart(Sender: TRtcConnection);
begin
  btnListen.Enabled:=false;
  btnStopListen.Enabled:=True;

end;

当停止监听事件触发时,改变按钮的状态:

procedure TForm2.TcpServerListenStop(Sender: TRtcConnection);
  begin
      btnStopListen.Enabled:=false;
      btnListen.Enabled:=true;

  end;

运行效果如下图:

用RealThinClient组件实现Tcp服务器(1)

相关文章: