【问题标题】:How to write and read Stream using indy 10.5.5 c++如何使用 indy 10.5.5 c++ 读写 Stream
【发布时间】:2012-05-31 00:37:29
【问题描述】:

您好,我已尝试使用此代码从服务器读取 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)

如何处理?

【问题讨论】:

  • 发生崩溃时您应该做的第一件事是在调试器中运行您的程序。它不仅可以帮助您确定崩溃的位置,还可以让您检查变量以帮助您找出崩溃的原因

标签: c++ c++builder indy10


【解决方案1】:

在调用 ReadStream() 之前,您没有实例化您的 TStream 对象。您的 TS 变量完全未初始化。 ReadStream() 不会为你创建TStream 对象,只会写入它,所以你必须事先自己创建TStream

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

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(); 
    } 
} 

【讨论】:

  • 嗨,雷米先生,我在TStream 上犯了大错,现在你又帮了我。我向你学习了很多。非常感谢
猜你喜欢
  • 2016-12-09
  • 2022-10-14
  • 1970-01-01
  • 2015-09-14
  • 2016-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
相关资源
最近更新 更多