0

ネットワーク経由で送信する前にストリームに変換するコレクションを使用して、 IdTCPServer と IdTCPClient の間でデータを交換しようとしています。残念ながら、どのように試しても、クライアントとサーバーの間でストリームを渡すことができないようです。コードは常にIdTCPClient1.IOHandler.ReadStream(myStream, -1, True)行でハングします。

私のコードの関連部分を以下に示します。

クライアント側

  with ClientDataModule do
  begin
    try
      try
        intStreamSize := StrToInt(IdTCPClient1.IOHandler.ReadLn); // Read stream size
        IdTCPClient1.IOHandler.ReadStream(myStream, -1, True);  // Read record stream
      finally
        ReadCollectionFromStream(TCustomer, myStream);
      end;
    except
      ShowMessage('Unable to read the record from stream');
    end;
  end;

サーバ側

    try
      try
        SaveCollectionToStream(ACustomer, MStream);
      finally
        MStream.Seek(0, soFromBeginning);
        IOHandler.WriteLn(IntToStr(MStream.Size));   // Send stream size
        IOHandler.Write(MStream, 0);        // Send record stream
      end;
    except
      ShowMessage('Unable to save the record to stream');
    end;

この問題を解決するためのご支援をいただければ幸いです。

ありがとう、

Jダニエル

4

1 に答える 1

1

AReadUntilDisconnectのパラメーターを True に設定していますReadStream()。これにより、接続が閉じられるまで読み取りを続けるように指示されます。代わりに、パラメーターを False に設定する必要があります。AByteCountまた、ストリーム サイズを個別に送信しているためReadStream()、実際に読み取る量を指定する必要があるため、パラメータでストリーム サイズを渡す必要があります。

これを試して:

クライアント:

with ClientDataModule do
begin
  try
    intStreamSize := StrToInt(IdTCPClient1.IOHandler.ReadLn);
    IdTCPClient1.IOHandler.ReadStream(myStream, intStreamSize, False);
    myStream.Position := 0;
    ReadCollectionFromStream(TCustomer, myStream);
  except
    ShowMessage('Unable to read the record from stream');
  end;
end;

サーバ:

try
  SaveCollectionToStream(ACustomer, MStream);
  MStream.Position := 0;
  IOHandler.WriteLn(IntToStr(MStream.Size));
  IOHandler.Write(MStream);
except
  ShowMessage('Unable to save the record to stream');
end;

プロトコルを変更できる場合は、次のように、ストリーム サイズを内部的に交換Write()できます。ReadStream()

クライアント:

with ClientDataModule do
begin
  try
    // set to True to receive a 64bit stream size
    // set to False to receive a 32bit stream stream
    IdTCPClient1.IOHandler.LargeStream := ...;

    IdTCPClient1.IOHandler.ReadStream(myStream, -1, True);
    myStream.Position := 0;
    ReadCollectionFromStream(TCustomer, myStream);
  except
    ShowMessage('Unable to read the record from stream');
  end;
end;

サーバ:

try
  SaveCollectionToStream(ACustomer, MStream);
  MStream.Position := 0;

  // set to True to send a 64bit stream size
  // set to False to send a 32bit stream stream
  IOHandler.LargeStream := ...;

  IOHandler.Write(MStream, 0, True);
except
  ShowMessage('Unable to save the record to stream');
end;
于 2011-11-22T20:14:58.847 に答える