Winsocket分发多个不同的数据包,如何标示,如何接收,求救
比如服务器发送不同的数据包,如何表述,客户端如何识别。给个实例讲讲
[解决办法]
//兄弟啊,我給你方法,你要舉一反三__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------void __fastcall TForm1::FormCreate(TObject *Sender){ IdTCPServer1->DefaultPort = 8001 ; IdTCPClient1->Host = "127.0.0.1" ; IdTCPClient1->Port = 8001 ; IdTCPServer1->Active = true ; IdTCPClient1->Connect();}//---------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){ IdTCPClient1->WriteLn("@#发送的数据#@"); IdTCPClient1->WriteLn("#&3860&#"); //以&#为整个数据的结尾符号}//---------------------------------------void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread){ AnsiString Str = AThread->Connection->ReadLn() ; Memo1->Lines->Add(Str) ;}//---------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender){ AnsiString s ; for(int i=0 ;i<Memo1->Lines->Count;i++) { s += Memo1->Lines->Strings[i] ; } int pos1 = s.Pos("@#"); int pos2 = s.Pos("#@"); int pos3 = s.Pos("#&"); int pos4 = s.Pos("&#"); if(pos1>0&&pos2>0) { Memo2->Lines->Add(s.SubString(pos1+2, pos2-pos1-2)); } else { ShowMessage("没有找到监测数据包识符,请继续接收数据"); } if(pos3>0&&pos4>0) { Memo2->Lines->Add(s.SubString(pos3+2,s.Length() -pos3 - 3)); } else { ShowMessage("没有找到控制数据包识符,请继续接收数据"); }}//---------------------------------------