It is not necessary if the client is TCP/IP is a short connection.
1 type2Pclientconns = ^tclientconns;//Customer Connection3Tclientconns =Record4 Clientid:integer;5Ip:string;6Portstring;7 Logintime:tdatetime;8 End;9 Ten type OneG_clientconnects:tdictionary<tidtcpconnection, pclientconns>;//Client Connection Dictionary A - procedureTservercontainer1.dstcpservertransport1connect - (event:tdstcpconnecteventobject); the var - P:pclientconns; - begin - Try + ifG_connnectcount >= G_maxconnnum Then - begin +Loginfo ('the number of client connections authorized by the system has been exceeded'); A tidtcpconnection (event.connection). Disconnect; at exit; - End; - InterlockedIncrement (g_connnectcount); - New (p); - ifAssigned (P) Then - begin inP^.clientid: =Event.Channel.ChannelInfo.Id; -P^.ip: =Event.Channel.ChannelInfo.ClientInfo.IpAddress; toP^.port: =Event.Channel.ChannelInfo.ClientInfo.ClientPort; +P^.logintime: =Now ; - G_clientconnects.add (Tidtcpconnection (event.connection), p); the PostMessage (Application.MainForm.Handle, Wm_adduser, WParam (p), * LParam (Tidtcpconnection (event.connection))); $ End;Panax Notoginseng except - exit; the End; + End; A the procedureTservercontainer1.dstcpservertransport1disconnect + (event:tdstcpdisconnecteventobject); - var $ P:pclientconns; $ begin - Try - ifG_connnectcount >=1 Then the interlockeddecrement (globalvar.g_connnectcount); -P: =g_clientconnects.items[tidtcpconnection (event.connection)];Wuyi ifAssigned (P) Then the begin -SendMessage (Application.MainForm.Handle, Wm_deluser, WParam (p),0); Wu G_clientconnects.remove (Tidtcpconnection (event.connection)); - End; About except $ exit; - End; - End; - A procedureTf_mainform.adduser (varmsg:tmessage); + var the P:pclientconns; - begin $ Try theLabel4.caption: =IntToStr (g_connnectcount); theP: =Pclientconns (Msg. WParam); the ifAssigned (P) Then the begin - Clientdataset1.append; inClientdataset1.fieldbyname ('ID'). Asinteger: =P^.clientid; theClientdataset1.fieldbyname ('IP'). Asstring: =P^.ip; theClientdataset1.fieldbyname ('Port'). Asstring: =P^.port; AboutClientdataset1.fieldbyname (' Time'). Asdatetime: =P^.logintime; theClientdataset1.fieldbyname ('Conn'). Asinteger: =Msg. LParam; the Clientdataset1.post; the End; + except -On E:exception Do the beginBayiLoginfo ('Tf_mainform.adduser---'+e.message); the exit; the End; - End; - End; the the procedureTf_mainform.deluser (varmsg:tmessage); the var the P:pclientconns; - begin the Try theLabel4.caption: =IntToStr (g_connnectcount); theP: =Pclientconns (Msg. WParam);94 ifAssigned (P) Then the begin the ifClientdataset1.findkey ([P^.clientid]) Then the Clientdataset1.delete;98 Dispose (p); About End; - except101On E:exception Do102 begin103Loginfo ('Tf_mainform.deluser---'+e.message);104 exit; the End;106 End;107 End;
Datasnap How to monitor the connection of clients