客户端:
两个memo,memohe,memoyou
三个button,buttonconnect,buttoncancel,buttonclose
意义同button名字
unit UnitChat;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls, ComCtrls;
type
TFormChat = class(TForm)
Label1: TLabel;
Label2: TLabel;
MemoHe: TMemo;
MemoYou: TMemo;
Label3: TLabel;
EditHeHost: TEdit;
ButtonConnect: TButton;
Label4: TLabel;
Label5: TLabel;
EditHePost: TEdit;
ClientSocketChat: TClientSocket;
StatusBarChat: TStatusBar;
ButtonCencel: TButton;
ButtonClose: TButton;
procedure ButtonConnectClick(Sender: TObject);
procedure ClientSocketChatConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketChatRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketChatError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure MemoYouKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ButtonCloseClick(Sender: TObject);
procedure ButtonCencelClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FormChat: TFormChat;
implementation
{$R *.DFM}
procedure TFormChat.ButtonConnectClick(Sender: TObject);
begin
ClientSocketChat.Host :=EditHeHost.Text ;
ClientSocketChat.Port :=strtoint(EditHePost.Text) ;
StatusBarChat.SimpleText := '正在连接' + EditHeHost.Text
+ ' 端口:' + EditHePost.Text;
ClientSocketChat.Open ;
ButtonConnect.Enabled:=False;
ButtonCencel.Enabled:=true;
end;
procedure TFormChat.ClientSocketChatConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
StatusBarChat.SimpleText := '已经连接' + EditHeHost.Text
+ ' 端口:' + EditHePost.Text;
end;
procedure TFormChat.ClientSocketChatRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
MemoHe.Lines.Add (ClientSocketChat.Socket.ReceiveText);
end;
procedure TFormChat.ClientSocketChatError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
StatusBarChat.SimpleText := '连接' + EditHeHost.Text
+ ' 端口:' + EditHePost.Text + '失败';
end;
procedure TFormChat.MemoYouKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_Return then
ClientSocketChat.Socket.SendText(MemoYou.Lines[MemoYou.Lines.Count - 1]);
end;
procedure TFormChat.ButtonCloseClick(Sender: TObject);
begin
ClientSocketChat.Close ;
Application.Terminate ;
end;
procedure TFormChat.ButtonCencelClick(Sender: TObject);
begin
ClientSocketChat.Close ;
ButtonConnect.Enabled:=true;
ButtonCencel.Enabled:=false;
end;
procedure TFormChat.FormCreate(Sender: TObject);
begin
end;
end.
服务器端:
unit UnitChat;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ScktComp, StdCtrls, ComCtrls;
type
TFormChat = class(TForm)
Label1: TLabel;
Label2: TLabel;
MemoHe: TMemo;
MemoYou: TMemo;
Label3: TLabel;
EditHeHost: TEdit;
ButtonStart: TButton;
Label4: TLabel;
Label5: TLabel;
EditHePost: TEdit;
StatusBarChat: TStatusBar;
ServerSocketChat: TServerSocket;
ButtonStop: TButton;
ButtonClose: TButton;
procedure ButtonStartClick(Sender: TObject);
procedure ServerSocketChatAccept(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocketChatClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure MemoYouKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ButtonStopClick(Sender: TObject);
procedure ButtonCloseClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FormChat: TFormChat;
implementation
{$R *.DFM}
procedure TFormChat.ButtonStartClick(Sender: TObject);
begin
if EditHePost.Text<>'' then
begin
ServerSocketChat.Port :=StrToInt(EditHePost.Text);
ServerSocketChat.Open ;
ButtonStart.Enabled :=False;
ButtonStop.Enabled :=True;
end;
end;
procedure TFormChat.ServerSocketChatAccept(Sender: TObject;
Socket: TCustomWinSocket);
begin
EditHeHost.Text := ServerSocketChat.Socket.RemoteHost ;
StatusBarChat.SimpleText := '已经连接' + EditHeHost.Text
+ ' 端口:' + EditHePost.Text;
end;
procedure TFormChat.ServerSocketChatClientRead(Sender: TObject;
Socket: TCustomWinSocket);
begin
MemoHe.Lines.Add (ServerSocketChat.Socket.ReceiveText);
end;
procedure TFormChat.MemoYouKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_Return then
ServerSocketChat.Socket.Connections[0].SendText(MemoYou.Lines
[MemoYou.Lines.Count - 1]);
end;
procedure TFormChat.ButtonStopClick(Sender: TObject);
begin
ServerSocketChat.Close ;
ButtonStart.Enabled :=true;
ButtonStop.Enabled :=False;
end;
procedure TFormChat.ButtonCloseClick(Sender: TObject);
begin
ServerSocketChat.Close ;
Application.Terminate ;
end;
procedure TFormChat.FormCreate(Sender: TObject);
begin
end;
end.