Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Oct : Re: OnBeforeCommandHandler - Indy v9
| Subject: | Re: OnBeforeCommandHandler - Indy v9 |
| Posted by: | "Jamie Dale" (jamie.da..@yahoo.com) |
| Date: | Tue, 30 Oct 2007 21:38:45 |
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:47267213$2@newsgroups.borland.com...
>
> "Jamie Dale" <jamie.dale@yahoo.com> wrote in message
> news:472666fe$1@newsgroups.borland.com...
>
>> It isn't the indy component which was giving me greif, it was the Indy
>> demo!
>
> I do not know what demo you are referring to.
BasicClientServer - Demonstrating how to use TIdTCPServer with and without
command handlers.
As you know, I've only recently upgraded from v8 to v9 so I'm still trying
to get my head around the best methods of doing things which I'd written to
work in v8..
>> The commandhandler server was using a case statement for
>> a TComboBox integer - unfortunately sending the command
>> with a parameter made the itemindex be returned as -1 - there
>> was no case for -1 and thus it wasn't reading using ReadLn!
>
> Why was it calling ReadLn() in the first place? What is the demo trying
> to do?
The client demo was demo'ing how to send a command, and receive the
response. The command is selected from a TComboBox.
Here, take a sneek-peak at the source for the procedure involved:
procedure TfrmMain.btnSendCommandClick(Sender: TObject);
var
LCommand, LInString : String;
LInInteger : integer;
Read: Boolean;
begin
LCommand := cboCommands.Text;
LInInteger := -1;
with IdTCPClient do
begin
try
WriteLn(LCommand);
case cboCommands.ItemIndex of
0: LInString := ReadLn; //Date
1: LInString := ReadLn; //Time
2: LInInteger := ReadInteger; //TickCount
3: LInString := ReadLn; //Doh! an unknown command
4: LInString := ReadLn; //Quit
end;
if LInInteger <> -1 then
LInString := IntToStr(LInInteger);
lbCommunication.Clear;
lbCommunication.Items.Add('We said -> ' + LCommand);
lbCommunication.Items.Add('Server said -> ' + LInString);
except
on E : Exception do
begin
LockControls(True);
ShowMessage(E.Message);
end;
end;
end;
end;
As you can see, if a custom command OR parameters were typed into the
TComboBox, the ItemIndex returned became -1 - Thus, ReadLn was not called.
Therefore, lbCommuncation.Items.Add('Server said -> ' + LInString) would
return a blank line (which is what part of my symptom was).
The problem which really annoyed me was that when sending parameters, they
were parsed, but due to the ComboBox.ItemIndex returning -1, it would not
ReadLn, and thus was printing a blank line in the client making it look like
the server wasn't parsing properly (Though when watching after
break-pointing, the server WAS writeLn'ing it).
To counter this, this is what I did: (with the server parsing params and
sending each one back seperately)
procedure TfrmMain.btnSendCommandClick(Sender: TObject);
var
LCommand, LInString : String;
LInInteger : integer;
Read: Boolean;
begin
LCommand := cboCommands.Text;
LInInteger := -1;
with IdTCPClient do
begin
try
WriteLn(LCommand);
if LInInteger <> -1 then
LInString := IntToStr(LInInteger);
lbCommunication.Clear;
lbCommunication.Items.Add('We said -> ' + LCommand);
repeat
LInString := ReadLn('', 100, -1);
If LInString <> '' then
lbCommunication.Items.Add('Server said -> ' + LInString)
//lbCommunication.Items.Insert(0,'We said -> ' + LCommand);
//lbCommunication.Items.Insert(0,'Server said -> ' + LInString);
until LInString = '';
except
on E : Exception do
begin
LockControls(True);
ShowMessage(E.Message);
end;
end;
end;
end;