BCB2010中IdUDPServer接收数据的问题
在IdUDPServer的UDPRead事件中,如何处理TBytes AData?比如说我想接收的内容是汉字。
void __fastcall TForm1::IdUDPServer1UDPRead(TIdUDPListenerThread *AThread, TBytes AData,
TIdSocketHandle *ABinding)
{
try
{
wchar_t *a = new wchar_t(AData.Length);
//a = &AData[0] ;
String s = a;
s = s.SubString(0,AData.Length);
Memo1->Lines->Add(ABinding->PeerIP +": " + s);
}
catch(...)
{
}
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
this->IdUDPClient1->Send("我发的是一串汉字!");
}
void __fastcall TForm1::IdUDPServer1UDPRead(TIdUDPListenerThread *AThread,
TBytes AData, TIdSocketHandle *ABinding)
{
Memo1->Lines->Add(BytesToString(AData, TEncoding::Unicode));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
IdUDPClient1->Send("我了个艹abc", TEncoding::Unicode);
}
void __fastcall TForm1::IdUDPServer1UDPRead(TIdUDPListenerThread *AThread, TBytes AData, TIdSocketHandle *ABinding)
{
for (int i = 0; i < AData.Length; i++)
{
Memo1->Lines->Add(IntToHex(AData[i], 2));
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
byte btBuffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
TByteDynArray bda;
int nLen = sizeof(btBuffer);
bda.set_length(nLen);
for (int i = 0; i < nLen; i++)
{
bda[i] = btBuffer[i];
}
IdUDPClient1->SendBuffer(bda);
}