在开始之前我们需要了解下这个Demo功能是啥
我们可以看到这是两个小project,左边的project有服务端和客户端1,右边的project只有一个客户端2
效果就是当两个客户端各自分别输入正确的IP地址和PORT端口,然后btconnect连接就可以连接到这个服务端
然后就可以发送消息,服务端的Memo1可以显示来自客户端发送信息的内容。
另外服务端也可以发送消息给两个客户端,做到双向传递信息。
两个客户端发送信息给服务端:
服务端接收消息反馈给客户端:
做的很粗糙,但是大概功能算是实现了
具体做法:
第一步、
建立一个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
代码如下: