以下是使用 Delphi 编写基于 Socket 的简单聊天程序的示例代码。在这个示例代码中,我们将创建一个基于 TCP 协议的服务器和客户端,客户端可以连接到服务器并发送和接收消息。具体实现如下:
- 服务端代码
在服务端代码中,我们需要使用 TServerSocket
和 TServerSocketThread
组件创建一个基于 TCP 协议的服务器。当有客户端连接到服务器时,服务器将为每个客户端创建一个独立的线程来处理与客户端的通信。服务器可以接收客户端发送的消息,并将其广播给所有连接到服务器的客户端。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Spin, IdBaseComponent, IdComponent, IdTCPServer;
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
Button1: TButton;
Button2: TButton;
PortEdit: TSpinEdit;
Memo1: TMemo;
StatusLabel: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
IdTCPServer1.DefaultPort := PortEdit.Value;
IdTCPServer1.Active := True;
StatusLabel.Caption := 'Server is running on port ' + IntToStr(PortEdit.Value);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
IdTCPServer1.Active := False;
StatusLabel.Caption := 'Server is stopped';
end;
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var vBuff: array[0..1023]of Char;
begin
while(AThread.Connection.InputBuffer.Size=0)do
begin
AThread.Connection.ReadFromStack();
AThread.Connection.CheckForDisconnect(True, True);
end;
AThread.Connection.ReadBuffer(vBuff,AThread.Connection.InputBuffer.Size);
Memo1.Lines.Add('接收数据:'+StrPas(vBuff));
end;
end.
2.客户端代码
在客户端代码中,我们需要使用 TClientSocket
组件创建一个基于 TCP 协议的客户端。客户端可以连接到服务器并发送和接收消息。当客户端接收到服务器发送的消息时,它将消息显示在消息框中。
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
StdCtrls, Spin;
type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
PortEdit: TSpinEdit;
MessageMemo: TMemo;
StatusLabel: TLabel;
HostEdit: TEdit;
IdTCPClient1: TIdTCPClient;
Button3: TButton;
MessageEdit: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure IdTCPClient1Status(ASender: TObject;
const AStatus: TIdStatus; const AStatusText: String);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
IdTCPClient1.Host := HostEdit.Text;
IdTCPClient1.Port := PortEdit.Value;
IdTCPClient1.Connect;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
IdTCPClient1.Disconnect;
end;
procedure TForm2.Button3Click(Sender: TObject);
begin
if IdTCPClient1.Connected then
begin
IdTCPClient1.WriteLn(MessageEdit.Text);
//IdTCPClient1.IOHandler.(MessageEdit.Text);
MessageEdit.Clear;
end;
end;
procedure TForm2.IdTCPClient1Status(ASender: TObject;
const AStatus: TIdStatus; const AStatusText: String);
begin
MessageMemo.Lines.Add(AStatusText);
end;
end.
以上就是使用 Delphi 编写基于 Socket 的简单聊天程序的示例代码。需要注意的是,这只是一个简单的示例程序,实际开发中可能需要对程序进行优化和改进,以满足更高的性能和稳定性要求。
效果如下所示:
![]() | ![]() |