Server Code for an Internet Access Control Application - Final Part of the Procedure
(Page 4 of 4 )
The final part of the procedure searches for the workstation on the context list and then sends the session information to that workstation:
List := form1.ts.Contexts.LockList;
try
for I := 0 to List.Count-1 do
begin
Context :=TMyContext(List[I]);
if Context.compname = theStation then
begin
try
Context.Connection.IOHandler.WriteLn('activate:'+theStation+'@'+thetime);
except
end;
Exit;
end;
end;
finally
form1.ts.Contexts.UnlockList;
end;
form1.memo1.Lines.add('error.');
end;
The "parsestring()" function is at its simplest a string handling function. It is responsible for parsing the strings that are sent between the server and client applications. This particular function will also be required by the client application according to the protocol requirements. Our custom protocol requires all communication to have the following format:
'activate:'+theStation+'@'+thetime
There are two delimiters, the colon(:) and the at(@) sign. Both of these are used to parse the communication into three parts. Below is the code that does the job. It is commented and easy to understand:
function ParseString(s : string; var Token1,Token2,Token3: string) :
boolean;
var
P1,P2 : integer;
begin
//store positions before and after the delimiters
P1 := Pos(':',s);
P2 := Pos('@',s);
//Test if both delimiters are present, in the right order and
//at least 1 char apart
if ((P1 > 0) and (P2 > 0) and (P2 > P1) and (Abs(P2-P1) > 1))
then begin
//store the different parts
Token1 := Copy(s,1,P1-1);
Token2 := Copy(s,P1+1,P2-P1-1);
Token3 := Copy(s,P2+1,Length(s)-P2);
Result := True; //valid string
end
else Result := False; //invalid string
end;
The parsestring() function is of the utmost importance to the overall communication between the server and the client. Without this function there will be a complete breakdown of communication. So it is very important that you understand how this function works and improve on it.
Conclusion
There are a lot more procedures in the server code that we will look at, but unfortunately we will not look at all of them. In the next article we will cover three more procedures that also play a prominent role in the functional requirement of the server application. If at any stage anything is unclear to you about the code, please do not hesitate to contact me.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |