Sever Side Chat Application with Borland Delphi/Indy - Receiving messages: listnames, all, takeshot, name, file
(Page 3 of 5 )
When a client sends a message it will be received (by the server) in this form: from@msg:to
from: represents the name of the client who sends the message
msg: represents the actual message or command
to: represents the name of the sender
When the server receives this message it will need to break it down into three parts to make sense of it. Put this function anywhere in the implementation section.
The following code makes this possible:
function ParseString(s : string; var str1,str2,str3: string) :
boolean;
var
P1,P2 : integer;
begin
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
str1 := Copy(s,1,P1-1);
str2 := Copy(s,P1+1,P2-P1-1);
str3 := Copy(s,P2+1,Length(s)-P2);
Result := True; //valid string
end
else Result := False; //invalid string
end;
All that this function does is take in a string - s : string – and break it down into three parts, based on two delimiters which are - @ AND : - So, if I send a message saying ‘hello mate’, the server will receive: fromme@hello mate:toyou. The parsestring function will take that message and break it down into sender, message, recipient.
Next: Handling Commands: Listnames, all, takeshot >>
More Delphi-Kylix Articles
More By Jacques Noah