unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdIPWatch, ScktComp, StdCtrls,
ExtCtrls;
type
TForm1 =
class(TForm)
Memo1: TMemo;
ServerSocket1: TServerSocket;
IdIPWatch1: TIdIPWatch;
lblip: TLabel;
Panel1: TPanel;
ListBox1: TListBox;
Edit1: TEdit;
Button1: TButton;
ClientSocket1: TClientSocket;
ListBox2: TListBox;
procedure FormActivate(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
function GetToken(aString, SepChar:
string; TokenNum: Byte):
string;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormActivate(Sender: TObject);
begin
lblip.Caption := IdIPWatch1.LocalIP;
clientsocket1.Host := IdIPWatch1.LocalIP;
clientsocket1.Active := true;
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var i :integer;
Text:
string;
nickname:
string;
message:
string;
begin
Text:=Socket.ReceiveText;
nickname := (Trim(GetToken(Text,'
: ',1)));
message := (Trim(GetToken(Text,'
: ',2)));
begin
Memo1.Lines.add(Timetostr(now) + '
: '+ nickname + '
: ' +
message);
For I := 0
to ServerSocket1.Socket.ActiveConnections - 1
do
begin
with ServerSocket1.Socket.Connections [I]
do
if (Connected)
then SendText(nickname + '
: ' +
message);
end;
end;
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
serversocket1.Close;
memo1.Lines.SaveToFile('
chat-log.txt');
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
ListBox1.Items.Delete(ListBox1.Items.IndexOf(Socket.RemoteAddress+'
('+Socket.RemoteHost+'
)'));
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var nickname:
string;
text:
string;
begin
Text := Socket.ReceiveText;
nickname := (Trim(GetToken(Text,'
: ',1)));
ListBox1.Items.Add(Socket.RemoteAddress + nickname);
end;
function TForm1.GetToken(aString, SepChar:
string; TokenNum: Byte):
string;
var
Token:
string;
StrLen: Byte;
Num: Byte;
EndofToken: Byte;
begin
StrLen := Length(aString);
Num := 1;
EndofToken := StrLen;
while ((Num <= TokenNum)
and (EndofToken <> 0))
do
begin
EndofToken := Pos(SepChar, aString);
if EndofToken <> 0
then
begin
Token := Copy(aString, 1, EndofToken - 1);
Delete(aString, 1, EndofToken);
Inc(Num);
end
else
Token := aString;
end;
if Num >= TokenNum
then
Result := Token
else
Result := '
';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if (clientsocket1.Active = true)
and (edit1.Text <> '
')
then clientsocket1.Socket.SendText('
console: ' + edit1.Text);
edit1.Clear;
edit1.SetFocus;
end;
end.