1 { KOL MCK } // Do not remove this line!
8 uses Windows
, Messages
, ShellAPI
, KOL
, KOLSocket
{$IFNDEF KOL_MCK}, mirror
, Classes
, mckCtrls
, Controls
, mckObjs
, mckSocket
, {$ENDIF}, Sockets
, WinSock
;{$ELSE}
14 {$I MCKfakeClasses.inc}
21 KOLProject
: TKOLProject
;
23 KOLApplet1
: TKOLApplet
;
25 EditBox1
: TKOLEditBox
;
28 ListView1
: TKOLListView
;
30 RichEdit1
: TKOLRichEdit
;
31 ServerSocket
: TKOLSocket
;
32 ClientSocket
: TKOLSocket
;
34 EditBox2
: TKOLEditBox
;
37 procedure ServerSocketAccept(SocketMessage
: TWMSocket
);
38 procedure ServerSocketClose(SocketMessage
: TWMSocket
);
39 procedure ServerSocketListen(SocketMessage
: TWMSocket
);
40 procedure ServerSocketError(SocketMessage
: TWMSocket
);
42 procedure AddSocket(fSocket
: PClientSocket
);
43 procedure ServerSend(fString
: string);
44 procedure BroadCast(fString
: string);
45 procedure ServerConnect
;
46 procedure ServerDisconnect
;
47 procedure SockError(Err
: String);
48 procedure ServerCloseConnect
;
50 procedure FreeGarbage
;
52 procedure ClientConnect
;
53 procedure ClientDisconnect
;
55 procedure KOLForm1FormCreate(Sender
: PObj
);
56 procedure KOLTimer1Timer(Sender
: PObj
);
57 procedure KOLForm1Destroy(Sender
: PObj
);
58 procedure Button1Click(Sender
: PObj
);
59 procedure Button2Click(Sender
: PObj
);
60 procedure EditBox1KeyUp(Sender
: PControl
; var Key
: Integer;
62 procedure ClientSocketConnect(SocketMessage
: TWMSocket
);
63 procedure ClientSocketRead(SocketMessage
: TWMSocket
);
64 procedure Button3Click(Sender
: PObj
);
65 procedure ClientSocketError(SocketMessage
: TWMSocket
);
66 procedure ClientSocketClose(SocketMessage
: TWMSocket
);
67 procedure Button4Click(Sender
: PObj
);
70 { Private declarations }
73 // m_ServerSocket: PAsyncSocket;
75 { Public declarations }
78 Form1
{$IFDEF KOL_MCK} : PForm1
{$ELSE} : TForm1
{$ENDIF} ;
79 constat
: boolean = false;
80 constatc
: boolean = false;
84 procedure NewForm1( var Result
: PForm1
; AParent
: PControl
);
89 {$IFNDEF KOL_MCK} {$R *.DFM} {$ENDIF}
97 procedure TForm1
.KOLForm1FormCreate(Sender
: PObj
);
99 Form
.StatusPanelRightX
[0]:=75;
100 Form
.StatusPanelRightX
[1]:=Form
.StatusPanelRightX
[0]+90;
101 m_GarbageList
:=NewList
;
107 procedure TForm1
.ServerConnect
;
109 Form
.StatusText
[0]:=PChar('Connected ['+Int2Str(ListView1
.Count
)+']');
110 Form
.StatusText
[1]:='Server'; /////
111 Button1
.Enabled
:=True;
112 EditBox1
.Enabled
:=True;
113 EditBox1
.Color
:=clWindow
;
119 procedure TForm1
.ServerDisconnect
;
121 if ListView1
.Count
>0 then
122 Form
.StatusText
[0]:=PChar('Connected ['+Int2Str(ListView1
.Count
)+']')
125 Form
.StatusText
[0]:=PChar('Disconnected');
126 Form
.StatusText
[1]:=''; /////
127 Button1
.Enabled
:=False;
128 EditBox1
.Enabled
:=False;
129 EditBox1
.Color
:=clBtnFace
;
136 procedure TForm1
.ServerSocketAccept(SocketMessage
: TWMSocket
);
138 tempSocket
: PClientSocket
;
141 tempSocket
:=NewClientSocket
;
142 ServerSocket
.DoAccept(PAsyncSocket(tempSocket
));
143 AddSocket(tempSocket
);
149 procedure TForm1
.ServerSocketClose(SocketMessage
: TWMSocket
);
155 // Removing this null procedure wil give you exept situation
156 procedure TForm1
.ServerSocketListen(SocketMessage
: TWMSocket
);
163 procedure TForm1
.ServerSocketError(SocketMessage
: TWMSocket
);
165 SockError(ServerSocket
.ErrToStr(SocketMessage
.SocketError
));
169 // Post Socket Errr Message
170 procedure TForm1
.SockError
;
172 Form
.StatusText
[2]:=PChar(Err
);
177 procedure TForm1
.AddSocket
;
182 ipadd
:=fSocket
.DoGetHostByAddr(PChar(fSocket
.IPAddress
));
183 if (ipadd
= '') then ipadd
:=fSocket
.IPAddress
;
184 ListView1
.LVAdd(Int2Str(dword(fSocket
)), 0, [], 0, 0, dword(fSocket
));
189 procedure TForm1
.BroadCast
;
191 RichEdit1
.Add(fString
+#13#10);
196 procedure TForm1
.ServerSend
;
202 for i
:=0 to ListView1
.Count
-1 do
204 t
:=PClientSocket(ListView1
.LVItemData
[i
]);
205 t
.SendString(fString
);
210 // Close All Connected Clients
211 procedure TForm1
.ServerCloseConnect
;
216 for i
:=0 to ListView1
.Count
-1 do
223 //Sub program Closing all clients
224 procedure TForm1
.SrvClosing
;
229 t
:=PClientSocket(ListView1
.LVItemData
[0]);
235 procedure TForm1
.FreeGarbage
;
240 if ingarbage
then exit
;
242 while m_GarbageList
.Count
>0 do
244 t
:=m_GarbageList
.Items
[0];
246 m_GarbageList
.Delete(0);
253 procedure TForm1
.KOLTimer1Timer(Sender
: PObj
);
262 procedure TForm1
.ClientConnect
;
264 {ClientSocket.DoClose;
265 ClientSocket.IPAddress := '127.0.0.1';
266 ClientSocket.DoConnect;}
267 Form
.StatusText
[0]:='Connected';
268 Form
.StatusText
[1]:='Client';
269 Form
.StatusText
[2]:='';
270 Button1
.Enabled
:=True;
271 EditBox1
.Enabled
:=True;
272 EditBox1
.Color
:=clWindow
;
273 Button3
.Caption
:='Discnnect';
280 procedure TForm1
.ClientDisconnect
;
282 Form
.StatusText
[0]:='Disconnected';
283 Form
.StatusText
[1]:='';
284 Form
.StatusText
[2]:='';
285 Button1
.Enabled
:=False;
286 EditBox1
.Enabled
:=False;
287 EditBox1
.Color
:=clBtnFace
;
288 Button3
.Caption
:='Connect';
291 //if ClientSocket <> nil then ClientSocket.DoClose;
295 procedure TForm1
.ClientSocketConnect(SocketMessage
: TWMSocket
);
301 procedure TForm1
.ClientSocketClose(SocketMessage
: TWMSocket
);
307 procedure TForm1
.ClientSocketRead(SocketMessage
: TWMSocket
);
311 //c:=''[1]; // might not have been initialized :)
312 RichEdit1
.Add(ClientSocket
.ReadLine(c
)+#13+#10);
313 //Socket1.(RichEdit1.Add);
320 // END OF CLIENT-SERVER EVENT
322 // Client/Server - POST MESSAGE 1-Server; 2-Client;
323 procedure TForm1
.Button1Click(Sender
: PObj
);
327 //RichEdit1.Add(m_ServerSocket.LocalIP);
328 if Length(EditBox1
.Text)>0 then
332 s
:='Server: '+EditBox1
.Text;
335 RichEdit1
.Add(s
+#13#10);
339 s
:='Client: '+EditBox1
.Text;
341 ClientSocket
.SendString(s
);
342 RichEdit1
.Add(s
+#13#10);
349 procedure TForm1
.EditBox1KeyUp(Sender
: PControl
; var Key
: Integer;
352 if Key
=VK_RETURN
then Button1Click(nil);
357 procedure TForm1
.Button2Click(Sender
: PObj
);
359 Form
.StatusText
[2]:='';
362 // Form.StatusText[2]:='';
363 ServerSocket
.DoListen
;
364 Button2
.Caption
:='Stop';
365 Button3
.Enabled
:=False;
367 constat
:= not constat
;
371 // Form.StatusText[2]:='';
373 ServerSocket
.DoClose
;
374 Button2
.Caption
:='Listen';
375 Button3
.Enabled
:=True;
377 constat
:= not constat
;
383 procedure TForm1
.Button3Click(Sender
: PObj
);
387 ClientSocket
.IPAddress
:=EditBox2
.Text;
388 ClientSocket
.DoConnect
;
389 Button3
.Caption
:='Disconnect';
390 constatc
:= not constatc
;
395 ClientSocket
.DoClose
;
396 Button3
.Caption
:='Connect';
397 constatc
:= not constatc
;
404 procedure TForm1
.KOLForm1Destroy(Sender
: PObj
);
415 ClientSocket
.DoClose
;
420 procedure TForm1
.ClientSocketError(SocketMessage
: TWMSocket
);
422 SockError(ClientSocket
.ErrToStr(SocketMessage
.SocketError
));
426 procedure TForm1
.Button4Click(Sender
: PObj
);
432 // sock: TAsyncSocket;
435 GetSockName(ClientSocket
.m_Handle
, Name
, len
);
436 str
:=int2str(ord(Name
.sin_addr
.S_un_b
.s_b1
))+'.'+
437 int2str(ord(Name
.sin_addr
.S_un_b
.s_b2
))+'.'+
438 int2str(ord(Name
.sin_addr
.S_un_b
.s_b3
))+'.'+
439 int2str(ord(Name
.sin_addr
.S_un_b
.s_b4
));