0

嗨,我尝试使用此代码从服务器读取 Stream

void __fastcall TForm1::Edit1KeyPress(TObject *Sender, wchar_t &Key)
{
   //TMemoryStream *TMS = new TMemoryStream;
   TStringStream *TSS = new TStringStream;
   AnsiString A,B;
   TStream *TS;
   INT64 Len;
   try
   {
       if (Key == VK_RETURN)
       {
          Beep(0,0);
          if(Edit1->Text == "mystream")
           {
               TCPClient1->IOHandler->WriteLn("mystream");
               Len = StrToInt(TCPClient1->IOHandler->ReadLn());
               TCPClient1->IOHandler->ReadStream(TS,Len,false);
               TSS->CopyFrom(TS,0);
               RichEdit1->Lines->Text = TSS->DataString;
               Edit1->Clear();
           }
       else
           {
              TCPClient1->IOHandler->WriteLn(Edit1->Text);
              A = TCPClient1->IOHandler->ReadLn();
              RichEdit1->Lines->Add(A);
              Edit1->Clear();
           }
       }
   }
   __finally
   {
       TSS->Free();
   }

}

编译器说,每次客户端尝试从服务器读取流。

First chance exception at $75D89617. Exception class EAccessViolation with message 'Access violation at address 500682B3 in module 'rtl140.bpl'. Read of address 00000018'. Process Project1.exe (6056)

如何处理?

4

1 回答 1

3

TStream调用ReadStream(). 您的TS变量完全未初始化。 ReadStream()不会TStream为您创建对象,只会写入它,因此您必须TStream事先创建自己。

鉴于您显示的代码,您可以TStream使用以下ReadString()方法完全替换:

void __fastcall TForm1::Edit1KeyPress(TObject *Sender, wchar_t &Key) 
{ 
    if (Key == VK_RETURN) 
    { 
        Beep(0,0); 
        if (Edit1->Text == "mystream") 
        { 
            TCPClient1->IOHandler->WriteLn("mystream"); 
            int Len = StrToInt(TCPClient1->IOHandler->ReadLn()); 
            RichEdit1->Lines->Text = TCPClient1->IOHandler->ReadString(Len); 
        } 
        else 
        { 
            TCPClient1->IOHandler->WriteLn(Edit1->Text); 
            String A = TCPClient1->IOHandler->ReadLn(); 
            RichEdit1->Lines->Add(A); 
        } 
        Edit1->Clear(); 
    } 
} 
于 2012-06-01T00:29:03.073 回答