Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Nov : Indy 9 TidTcpClient / TidTcpServer stream problems

www.cryer.info
Managed Newsgroup Archive

Indy 9 TidTcpClient / TidTcpServer stream problems

Subject:Indy 9 TidTcpClient / TidTcpServer stream problems
Posted by:"RandomAccess" (n..@tellingyou.com)
Date:Mon, 5 Nov 2007 10:30:26

I'm trying to have my Client send a data stream to the server,  but the
server keeps giving Stream Read Error exception.

I can't see what I'm doing wrong,  and any help would be appreciated.
Source is below,  and the exception source line is indicated in the server
side
code.

Best Regards


Client Side
===========================

//------------------------------------------------------------------------------
// Only call this if item was actually modified,
// Otherwise,  simply call CS_RQ_UnlockTableItem
function TMainForm.CS_NOT_UpdateTableItem(Table : TStorage_Table;
                                          Item  : TStorageItem) : Boolean;
var
Stream : TMemoryStream;
begin
  Screen.Cursor := crHourGlass;
  try
    result := (Client.SendCmd('NOT_UpdateItem ' +
                              Table.ClassName + ' ' +
                              Item.HashStr) = 997);
    if result then
    try
      Stream := TMemoryStream.Create;
      if Item.Write(Stream) then
      begin
        Stream.Position := 0;
        Client.WriteStream(Stream);
      end;
    finally
      Stream.Free;
    end;
  finally
    Screen.Cursor := crDefault;
  end;
end;


Server Side

//------------------------------------------------------------------------------
// Command : NOT_UpdateItem
procedure Tcs_Struct.NOT_UpdateItem(ASender: TIdCommand);
var
Table      : TStorage_Table;
Item       : TStorageItem;
HashStr    : String;
i          : Longint;
aHash      : Cardinal;
s          : String;
Connection : Tcs_Connection;
Stream     : TMemoryStream;
begin
  Connection := ThreadConnection[ASender.Thread];
  if Connection = nil then
  begin
    ASender.Reply.SetReply(-997, 'Unrecognized Connection');
    ASender.SendReply;
    exit;
  end;

  Table := nil;
  s := ASender.Params[0];

  // Find the Table
  if s = 'TStock' then
    Table := fStock;

  if Table = nil then
  begin
    ASender.Reply.SetReply(9999, 'Invalid Table Identifier');
    ASender.SendReply;
    exit;
  end;

  HashStr := ASender.Params[1];
  aHash   := StrToInt64Def(HashStr, -1);
  Item    := TStorageItem(Table.HashTable.HashObject[aHash]);
  if Item = nil then
  begin
    ASender.Reply.SetReply(9999, 'Invalid Item Identifier');
    ASender.SendReply;
    exit;
  end;

  // Read the updated data sent by the client
  Stream := TMemoryStream.Create;
  try
    ASender.Reply.SetReply(997, '');
    ASender.SendReply;


   // ####### EXCEPTION OCCURS BELOW #######
    ASender.Thread.Connection.ReadStream(Stream);


Stream.Position := 0;
    Item.Read(Stream);
    fMatStock.storage_WriteListItem(Item);
  finally
    Stream.Free;
    Item.UnLock(Connection.fUserHash);
  end;
end;

Replies:

www.cryer.info
Managed Newsgroup Archive