delphi XE8 TDSServer服务端怎样接收客户端消息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了delphi XE8 TDSServer服务端怎样接收客户端消息相关的知识,希望对你有一定的参考价值。

参考技术A 具体做法:
第一步、

建立一个Project新建From1拖入如下控件TServerSocket,TClientSocket,TMemo2个,Label四个,TEdit两个
TButton四个,TStatusBar。然后设置属性,ServerSocket1的Action为True;ServerSocket1的Port为1025
代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ScktComp;

type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
Memo2: TMemo;
StatusBar1: TStatusBar;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
btnConnect: TButton;
btnSent: TButton;
btnDisconnect: TButton;
Button1: TButton;
Memo1: TMemo;
Label3: TLabel;
Label4: TLabel;
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure btnConnectClick(Sender: TObject);
procedure btnDisconnectClick(Sender: TObject);
procedure btnSentClick(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private
Private declarations
public
Public declarations
end;

var
Form1: TForm1;

implementation

$R *.dfm

服务端代码////
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.SimpleText := 'connect';
end;

procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.SimpleText := 'disconnect';
end;

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('悄悄的对你说'+ Socket.ReceiveText);
end;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin

for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do
Serversocket1.socket.connections[i].SendText(Memo1.Text);//服务端向客户端发送信息

end;

////客户端代码////
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.SimpleText:='连接成功';//状态栏显示状态
end;

procedure TForm1.btnConnectClick(Sender: TObject);//输入IP和PORT连接
begin
if not ClientSocket1.Active then
begin
ClientSocket1.Host := Edit1.Text;
ClientSocket1.Port := StrToInt(Edit2.Text);
ClientSocket1.Open;
(Sender as TButton).Enabled := false;
btnDisconnect.Enabled := true;
end;
end;

procedure TForm1.btnDisconnectClick(Sender: TObject);//断开连接
begin
if ClientSocket1.Active then
begin
ClientSocket1.Close;
btnDisconnect.Enabled := false;
btnConnect.Enabled := true;
end;
end;

procedure TForm1.btnSentClick(Sender: TObject);//连接成功后发送数据给服务端
begin
if ClientSocket1.Active then
ClientSocket1.Socket.SendText(Memo2.Text)
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);//客户端监听来自服务端的数据
begin
Memo2.Lines.Add(Socket.ReceiveText);
end;

end.

第二步、

再新建一个project作为客户端2 建立Form1拖入控件TClientSocket,TMemo,TEdit两个,Label三个,TButton三个,TStatusBar

代码如下:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, ScktComp;

type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
Memo2: TMemo;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
btnConnect: TButton;
StatusBar1: TStatusBar;
btnSent: TButton;
btnDisconnect: TButton;
Label3: TLabel;
procedure ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procedure btnConnectClick(Sender: TObject);
procedure btnDisconnectClick(Sender: TObject);
procedure btnSentClick(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private
Private declarations
public
Public declarations
end;

var
Form1: TForm1;

implementation

$R *.dfm

procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.SimpleText:='连接成功';
end;

procedure TForm1.btnConnectClick(Sender: TObject);
begin
if not ClientSocket1.Active then
begin
ClientSocket1.Host := Edit1.Text;
ClientSocket1.Port := StrToInt(Edit2.Text);
ClientSocket1.Open;
(Sender as TButton).Enabled := false;
btnDisconnect.Enabled := true;
end;
end;

procedure TForm1.btnDisconnectClick(Sender: TObject);
begin
if ClientSocket1.Active then
begin
ClientSocket1.Close;
btnDisconnect.Enabled := false;
btnConnect.Enabled := true;
end;
end;

procedure TForm1.btnSentClick(Sender: TObject);
begin
if ClientSocket1.Active then
ClientSocket1.Socket.SendText(Memo2.Text)
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo2.Lines.Add(Socket.ReceiveText);
end;

end.
————————————————
版权声明:本文为CSDN博主「尘中客」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/s371795639/article/details/53976864

delphi xe5 android 开发数据访问手机端 解决乱码的办法

经过测试,将sqlserver里的字段由varchar 或者char  改为  nvarchar 或者nchar

然后在手机端的clientdataset 增加字段的时候数据类型选择widestring

 

以上是关于delphi XE8 TDSServer服务端怎样接收客户端消息的主要内容,如果未能解决你的问题,请参考以下文章

DELPHI XE8 远程调试

Delphi XE8中开发DataSnap程序常见问题和解决方法 (-)启动创建好的DBExpress工程时候报错了!

Delphi XE8 TStyleBook的使用

Delphi XE8帮助中的REST相关内容。

求救,这个delphi xe8要怎么破解

Delphi XE8,C++ Builder XE8,RAD Studio XE8 官方 ISO 文件下载,附激活工具