SendCmd()
サーバーからの応答を読み取るため、サーバーが実際に2つの別々の応答を送信GetResponse()
しない限り、後で呼び出さないでください。SendCmd()
通常、応答は次の形式を取ります。
<Response Code> <Optional Text>
応答コードが数字またはテキストキーワードのいずれかである場合。
サーバーが数値の応答コードを送信する場合は、次のように処理します。
サーバ:
// sends:
//
// 200 1
//
ASender.Reply.SetReply(200, '1');
クライアント:
if TCPclient.SendCmd(theMessage) = 200 then
Value := StrToInt(TCPclient.LastCmdResult.Text.Text);
または:
// raises an exception if a non-200 response is received
TCPclient.SendCmd(theMessage, 200);
Value := StrToInt(TCPclient.LastCmdResult.Text.Text);
サーバーがテキスト応答コードを送信する場合は、次のように処理します。
サーバ:
// sends:
//
// OK 1
//
ASender.Reply.SetReply('OK', '1');
クライアント:
if TCPclient.SendCmd(theMessage, '') = 'OK' then
Value := StrToInt(TCPclient.LastCmdResult.Text.Text);
または:
// raises an exception if a non-OK response is received
TCPclient.SendCmd(theMessage, ['OK']);
Value := StrToInt(TCPclient.LastCmdResult.Text.Text);
応答のオプションのテキスト(存在する場合)は、TCPclient.LastCmdResult.Text
プロパティでアクセスできます。これは、TStrings
次の形式で複数行の応答を送信できるためです。
<Response Code>-<Optional Text>
<Response Code>-<Optional Text>
...
<Response Code> <Optional Text>
サーバ:
// sends:
//
// 200-The value is
// 200 1
//
ASender.Reply.SetReply(200, 'The value is');
ASender.Reply.Text.Add('1');
クライアント:
TCPclient.SendCmd(theMessage, 200);
Value := StrToInt(TCPclient.LastCmdResult.Text[1]);
このフォームの回答の後に、2番目の複数行のテキストを送信することもできます。
<Response Code> <Optional Text>
<Secondary Text>
.
サーバ:
// sends:
//
// 200 Data follows
// Hello world
// How are you?
// .
//
ASender.Reply.SetReply(200, 'Data follows');
ASender.Reply.Response.Add('Hello world');
ASender.Reply.Response.Add('How are you?');
クライアント:
TCPclient.SendCmd(theMessage, 200);
TCPclient.IOHandler.Capture(SomeTStringsObj);