delphi XE8 TDSServer服务端怎样接收客户端消息
发布网友
发布时间:2022-12-22 08:35
我来回答
共1个回答
热心网友
时间:2024-11-18 20:31
具体做法:
第一步、
建立一个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;
procere ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procere ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procere ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procere Button1Click(Sender: TObject);
procere ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procere btnConnectClick(Sender: TObject);
procere btnDisconnectClick(Sender: TObject);
procere btnSentClick(Sender: TObject);
procere ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
服务端代码////
procere TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.SimpleText := 'connect';
end;
procere TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.SimpleText := 'disconnect';
end;
procere TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('悄悄的对你说'+ Socket.ReceiveText);
end;
procere 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;
////客户端代码////
procere TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.SimpleText:='连接成功';//状态栏显示状态
end;
procere 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;
procere TForm1.btnDisconnectClick(Sender: TObject);//断开连接
begin
if ClientSocket1.Active then
begin
ClientSocket1.Close;
btnDisconnect.Enabled := false;
btnConnect.Enabled := true;
end;
end;
procere TForm1.btnSentClick(Sender: TObject);//连接成功后发送数据给服务端
begin
if ClientSocket1.Active then
ClientSocket1.Socket.SendText(Memo2.Text)
end;
procere 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;
procere ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
procere btnConnectClick(Sender: TObject);
procere btnDisconnectClick(Sender: TObject);
procere btnSentClick(Sender: TObject);
procere ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procere TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBar1.SimpleText:='连接成功';
end;
procere 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;
procere TForm1.btnDisconnectClick(Sender: TObject);
begin
if ClientSocket1.Active then
begin
ClientSocket1.Close;
btnDisconnect.Enabled := false;
btnConnect.Enabled := true;
end;
end;
procere TForm1.btnSentClick(Sender: TObject);
begin
if ClientSocket1.Active then
ClientSocket1.Socket.SendText(Memo2.Text)
end;
procere 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