2012-05-31 40 views
0

喜读流我已经尝试从服务器使用此代码如何编写和使用印10.5.5 C++

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) 

如何处理?

+0

得到一个崩溃的时候,你应该做的第一件事,就是要在一个调试器中运行您的程序。它不仅可以帮助您查明崩溃的位置,还可以让您检查变量以帮助您找出坠毁的_reason_。 –

回答

3

在致电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(); 
    } 
} 
+0

嗨MR.Remy 我对'TStream'犯了很大的错误,现在你又帮了我一次。我向你学习很多。非常感谢你 – Adhy