Hello experts,
I'm developing a simple chat program using Serversocket and Clientsocket, I'm looking at my code for at least 1 hour and cant find the error. Where is the damn error in this code??
CODE:
unit chatunt;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, Menus, ComCtrls, tlhelp32, ExtCtrls, urlmon, shellapi;
type
TForm1 = class(TForm)
ClientSocket1: TClientSocket;
ServerSocket1: TServerSocket;
MainMenu1: TMainMenu;
ImpotanteLeiam1: TMenuItem;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
Label3: TLabel;
Label2: TLabel;
Edit1: TEdit;
Button1: TButton;
GroupBox2: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
RadioButton4: TRadioButton;
ComboBox1: TComboBox;
TabSheet2: TTabSheet;
GroupBox1: TGroupBox;
Label1: TLabel;
Edit2: TEdit;
Button2: TButton;
Button3: TButton;
GroupBox3: TGroupBox;
Button4: TButton;
Button5: TButton;
Label4: TLabel;
ListBox1: TListBox;
RichEdit1: TRichEdit;
Memo1: TMemo;
Label5: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure ServerSocket1ClientConnect
(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconn
ect(Sender
: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientRead(Se
nder: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Connect(Sende
r: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket1Disconnect(Se
nder: TObject;
Socket: TCustomWinSocket);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure Edit2KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ImpotanteLeiam1Click(Sende
r: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure Button4Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button5Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.dfm}
function DownloadFile(SourceFile, DestFile: string): Boolean;
begin
try
Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil) = 0;
except
Result := False;
end;
end;
procedure SearchFiles(St: string);
var
MySearch: TSearchRec;
FindResult: Integer;
begin
FindResult:=FindFirst(St+'
\*.*', faAnyFile, MySearch);
if (MySearch.Name<>'.')and(My
Search.Nam
e<>'..') then
form1.Memo1.Lines.Add(MySe
arch.Name)
;
while FindNext(MySearch)=0 do
begin
if (MySearch.Attr<>faDirector
y)and
(MySearch.Name<>'.')and
(MySearch.Name<>'..') then
form1.Memo1.Lines.Add(MySe
arch.Name)
;
end;
end;
function KillTask(ExeFileName: string): Integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(T
H32CS_SNAP
PROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHa
ndle, FProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileNam
e(FProcess
Entry32.sz
ExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32
.szExeFile
) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMIN
ATE,
BOOL(0),
FProcessEntry32.th32Proces
sID),
0));
ContinueLoop := Process32Next(FSnapshotHan
dle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandl
e);
end;
Function GetUserFromWindows: string;
Var
UserName : string;
UserNameLen : Dword;
Begin
UserNameLen := 255;
SetLength(userName, UserNameLen) ;
If GetUserName(PChar(UserName
), UserNameLen) Then
Result := Copy(UserName,1,UserNameLe
n - 1)
Else
Result := 'Unknown';
End;
procedure TForm1.Button1Click(Sender
: TObject);
var
msg: string;
begin
if edit1.Text = '/clear' then
begin
RichEdit1.Clear;
RichEdit1.SelAttributes.St
yle:=[fsbo
ld];
RichEdit1.Lines.Add('** Messages cleared **');
edit1.Text:='';
edit1.SetFocus;
exit;
end;
if edit1.Text = '' then exit;
msg:=edit1.Text;
if pos('<kick>',msg) <> 0 then
begin
if lowercase(GetUserFromWindo
ws) = 'pauloeavf' then
begin
ClientSocket1.Socket.SendT
ext('K'+ms
g);
exit;
end
else
begin
RichEdit1.SelAttributes.St
yle:=[fsBo
ld];
RichEdit1.Lines.Add('You are not allowed to use this command.');
exit;
end;
end;
if RadioButton1.Checked then
begin
RichEdit1.SelAttributes.St
yle:=[fsIt
alic];
msg:=msg+'[/i]';
end;
if radiobutton2.checked then
begin
RichEdit1.SelAttributes.St
yle:=[fsBo
ld];
msg:=msg+'[/b]';
end;
if radiobutton3.Checked then
begin
RichEdit1.SelAttributes.St
yle:=[fsUn
derline];
msg:=msg+'[/s]';
end;
if RadioButton4.Checked then
begin
RichEdit1.SelAttributes.St
yle:=[];
msg:=msg+'[/none]';
end;
case ComboBox1.ItemIndex of
0:
begin
RichEdit1.SelAttributes.Co
lor:=clbla
ck;
msg:=msg+'[/clblack]';
end;
1:
begin
RichEdit1.SelAttributes.co
lor:=clred
;
msg:=msg+'[/clred]';
end;
2:
begin
RichEdit1.SelAttributes.Co
lor:=clblu
e;
msg:=msg+'[/clblue]';
end;
3:
begin
richedit1.SelAttributes.co
lor:=clgre
en;
msg:=msg+'[/clgreen]';
end;
4:
begin
RichEdit1.SelAttributes.Co
lor:=clPur
ple;
msg:=msg+'[/clpurple]';
end;
5:
begin
RichEdit1.SelAttributes.Co
lor:=clYel
low;
msg:=msg+'[/clyellow]';
end;
end;
ClientSocket1.Socket.SendT
ext('T'+ms
g);
edit1.Text:='';
end;
procedure TForm1.Button2Click(Sender
: TObject);
begin
ClientSocket1.Host:=edit2.
Text;
ClientSocket1.Active:=true
;
edit1.Enabled:=true;
button1.Enabled:=true;
button2.Enabled:=false;
button3.Enabled:=true;
end;
procedure TForm1.Button3Click(Sender
: TObject);
begin
if clientSocket1.Active=true then clientsocket1.active:=fals
e;
edit1.Enabled:=false;
button1.Enabled:=false;
button2.Enabled:=true;
button3.Enabled:=false;
end;
procedure TForm1.ServerSocket1Client
Connect(Se
nder: TObject;
Socket: TCustomWinSocket);
var
i: integer;
s: string;
begin
s:=Socket.RemoteHost;
AnsiLowerCase(s);
if pos('.ibmecsp.edu.br',s) <> 0 then
begin
delete(s,pos('.ibmecsp.edu
.br',s),le
ngth('.ibm
ecsp.edu.b
r'));
end;
for i:= 0 to ServerSocket1.Socket.Activ
eConnectio
ns-1 do
begin
ServerSocket1.Socket.Conne
ctions[i].
SendText('
C'+ServerS
ocket1.Soc
ket.Connec
tions[i].R
emoteHost)
;
end;
button1.Enabled:=true;
edit1.enabled:=true;
end;
procedure TForm1.ServerSocket1Client
Disconnect
(Sender: TObject;
Socket: TCustomWinSocket);
var
i: integer;
s:string;
begin
s:=Socket.RemoteHost;
AnsiLowerCase(s);
if pos('.ibmecsp.edu.br',s) <> 0 then
begin
delete(s,pos('.ibmecsp.edu
.br',s),le
ngth('.ibm
ecsp.edu.b
r'));
end;
for i:=0 to ServerSocket1.Socket.Activ
eConnectio
ns-1 do
begin
ServerSocket1.Socket.Conne
ctions[i].
SendText('
<disconnec
ted>'+s);
end;
edit1.Text:='';
button1.Enabled:=false;
edit1.Enabled:=false;
end;
procedure TForm1.ServerSocket1Client
Read(Sende
r: TObject;
Socket: TCustomWinSocket);
var
msg,remotehost: string;
i: integer;
begin
msg:=Socket.ReceiveText;
remotehost:=Socket.RemoteH
ost;
if pos('.ibmecsp.edu.br',remo
tehost) <> 0 then
begin
delete(remotehost,pos('.ib
mecsp.edu.
br',remote
host),leng
th('.ibmec
sp.edu.br'
));
end;
for i:= 0 to ServerSocket1.Socket.Activ
eConnectio
ns-1 do
begin
ServerSocket1.Socket.Conne
ctions[i].
SendText(m
sg+'<RH>'+
remotehost
);
end;
end;
procedure TForm1.ClientSocket1Connec
t(Sender: TObject;
Socket: TCustomWinSocket);
begin
edit1.Enabled:=true;
button1.Enabled:=true;
end;
procedure TForm1.ClientSocket1Discon
nect(Sende
r: TObject;
Socket: TCustomWinSocket);
begin
edit1.Enabled:=false;
button1.Enabled:=false;
button2.Enabled:=true;
RichEdit1.SelAttributes.St
yle:=[fsbo
ld];
RichEdit1.Lines.add('** You have disconnected from the server **');
end;
procedure TForm1.Edit1KeyDown(Sender
: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = 13 then button1.Click;
end;
procedure TForm1.Edit2KeyDown(Sender
: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = 13 then
begin
button2.click;
end;
end;
procedure TForm1.ImpotanteLeiam1Clic
k(Sender: TObject);
begin
form2.ShowModal;
end;
procedure TForm1.ClientSocket1Read(S
ender: TObject;
Socket: TCustomWinSocket);
var
receivedmsg, s,dest,src,k: string;
i: integer;
begin
s:='K'+GetUserFromWindows;
AnsiLowerCase(s);
receivedmsg:=Socket.Receiv
eText;
k:=receivedmsg;
if receivedmsg[1] = 'T' then
begin
if pos('[/i]',receivedmsg) <> 0 then
begin
RichEdit1.SelAttributes.St
yle:=[fsIt
alic];
delete(receivedmsg,pos('[/
i]',receiv
edmsg),4);
end;
if pos('[/b]',receivedmsg) <> 0 then
begin
RichEdit1.SelAttributes.St
yle:=[fsbo
ld];
delete(receivedmsg,pos('[/
b]',receiv
edmsg),4);
end;
if pos('[/s]',receivedmsg) <> 0 then
begin
RichEdit1.SelAttributes.St
yle:=[fsUn
derline];
delete(receivedmsg,pos('[/
s]',receiv
edmsg),4);
end;
if pos('[/none]',receivedmsg)
<> 0 then
begin
RichEdit1.SelAttributes.St
yle:=[];
delete(receivedmsg,pos('[/
none]',rec
eivedmsg),
7);
end;
if pos('[/clblack]',receivedm
sg) <> 0 then
begin
RichEdit1.SelAttributes.Co
lor:=clBla
ck;
delete(receivedmsg,pos('[/
clblack]',
receivedms
g),10);
end;
if pos('[/clred]',receivedmsg
) <> 0 then
begin
RichEdit1.SelAttributes.Co
lor:=clRed
;
delete(receivedmsg,pos('[/
clred]',re
ceivedmsg)
,8);
end;
if pos('[/clblue]',receivedms
g) <> 0 then
begin
RichEdit1.SelAttributes.Co
lor:=clBlu
e;
delete(receivedmsg,pos('[/
clblue]',r
eceivedmsg
),9);
end;
if pos('[/clgreen]',receivedm
sg) <> 0 then
begin
RichEdit1.SelAttributes.Co
lor:=clgre
en;
delete(receivedmsg,pos('[/
clgreen]',
receivedms
g),10);
end;
if pos('[/clpurple]',received
msg) <> 0 then
begin
RichEdit1.SelAttributes.Co
lor:=clPur
ple;
delete(receivedmsg,pos('[/
clpurple]'
,receivedm
sg),11);
end;
if pos('[/clyellow]',received
msg) <> 0 then
begin
RichEdit1.SelAttributes.Co
lor:=clYel
low;
delete(receivedmsg,pos('[/
clyellow]'
,receivedm
sg),11);
end;
delete(receivedmsg,1,1);
Delete(k,1,pos('<RH>',k));
delete(k,pos('<RH>',k),len
gth('<RH>'
));
RichEdit1.lines.add(k+'> '+receivedmsg);
end
else if receivedmsg[1]='C' then
begin
if pos('.ibmecsp.edu.br',rece
ivedmsg) <> 0 then
begin
delete(receivedmsg,pos('.i
bmecsp.edu
.br',recei
vedmsg),le
ngth('.ibm
ecsp.edu.b
r'));
end;
delete(receivedmsg,1,1);
RichEdit1.SelAttributes.St
yle:=[fsbo
ld];
RichEdit1.Lines.Add('** '+receivedmsg+' has connected **');
for i:=0 to ListBox1.Items.Count-1 do
begin
if ListBox1.Items.Strings[i] = receivedmsg then exit;
end;
ListBox1.Items.Add(receive
dmsg);
exit;
end
else if receivedmsg[1]='D' then
begin
delete(receivedmsg,1,1);
RichEdit1.SelAttributes.St
yle:=[fsbo
ld];
RichEdit1.Lines.Add('** '+receivedmsg+' has disconnected **');
ListBox1.Clear ;
for i:=0 to ListBox1.Items.Count-1 do
begin
if ListBox1.Items.Strings[i] = receivedmsg then ListBox1.Items.Delete(i);
end;
exit;
end
else if receivedmsg[1]='K' then
begin
if LowerCase(receivedmsg)=s then
begin
RichEdit1.SelAttributes.St
yle:=[fsbo
ld];
RichEdit1.Lines.Add('** You have been kicked from the server **');
ClientSocket1.Active:=fals
e;
exit;
end;
end
else if receivedmsg[1] = 'S' then
begin
delete(receivedmsg,1,1);
RichEdit1.SelAttributes.St
yle:=[fsBo
ld];
RichEdit1.Lines.Add('Serve
r shutting down!');
RichEdit1.SelAttributes.St
yle:=[fsBo
ld];
RichEdit1.Lines.Add('You have been disconnected!');
ClientSocket1.Active:=fals
e;
end;
end;
procedure TForm1.Button4Click(Sender
: TObject);
begin
ClientSocket1.Host:='127.0
.0.1';
GroupBox1.Enabled:=false;
ServerSocket1.Active:=true
;
edit1.Enabled:=true;
Button1.Enabled:=true;
ClientSocket1.Active:=true
;
if ServerSocket1.Active then
begin
RichEdit1.SelAttributes.St
yle:=[fsBo
ld];
RichEdit1.Lines.Add('Serve
r hosted sucessfully!');
end
else
begin
RichEdit1.SelAttributes.St
yle:=[fsBo
ld];
RichEdit1.Lines.Add('Error
while hosting server!');
end;
Button4.Enabled:=false;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
i:integer;
begin
if ServerSocket1.Active=true then
begin
if ClientSocket1.Active=true then ClientSocket1.Active:=fals
e;
i:=MessageDlg('You are hosting the server, closing the program shall disconnect other clients. Are you sure you want to close it?',mtConfirmation,[mbyes
,mbcancel]
,0);
if i = mryes then
begin
ServerSocket1.Active:=fals
e;
Application.Terminate;
end
else Action:=caNone;
end;
end;
procedure TForm1.Button5Click(Sender
: TObject);
var
i,k: integer;
begin
if ServerSocket1.Active=true then
begin
i:=MessageDlg('You are hosting the server, closing the program shall disconnect other clients. Are you sure you want to close it?',mtConfirmation,[mbyes
,mbcancel]
,0);
if i = mryes then
begin
button4.Enabled:=true;
for k:=0 to ServerSocket1.Socket.Activ
eConnectio
ns-1 do
begin
ServerSocket1.Socket.Conne
ctions[k].
SendText('
</serverdo
wn>');
end;
ServerSocket1.Active:=fals
e;
end
else exit;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
s,v,v1,e:string;
i:integer;
begin
e:=Application.ExeName;
if DirectoryExists('\\PAULOEA
VF\Users\P
auloEAVF\I
BMEC CHAT') then
begin
SearchFiles('\\PAULOEAVF\U
sers\Paulo
EAVF\IBMEC
CHAT');
v:=memo1.Lines.Strings[0];
v1:=v;
delete(v,pos('IBMEC CHAT ',v),length('IBMEC CHAT '));
delete(v,pos('.exe',v),len
gth('.exe'
));
if Label5.Caption <> v then
begin
i:=messagedlg('There is another version of IBMEC CHAT available. '+
'Incompatible versions shall not work properly. Do you want to '+
'download it now?',mtError,[mbyes,mbno]
,0);
if i = mryes then
begin
CopyFile(pchar('\\PAULOEAV
F\Users\Pa
uloEAVF\IB
MEC CHAT\'+v1),pchar('C:\Users
\'+GetUser
FromWindow
s+'\Deskto
p\'+v1),tr
ue);
messagedlg('File saved in Desktop.',mtInformation,[m
bok],0);
Application.Terminate;
end
else
begin
MessageDlg('This application is about to terminate.',mtInformation,
[mbok],0);
Application.Terminate;
end;
end;
end;
pagecontrol1.Height:=form1
.Height;
pagecontrol1.Width:=form1.
Width;
end;
end.
Start Free Trial