客户端:
//---------------------------------------------------------------------------
bool __fastcall TModule::ApplyUpDate(TClientDataSet *DataSet)
{
bool Result = true;
if (0 < DataSet->ChangeCount)
if (GetConnected())
{
Result = 0 == DataSet->ApplyUpdates(-1);
TDataSource *Source = DataSet->MasterSource;
DataSet->MasterSource = NULL;
if (0 == DataSet->ChangeCount) DataSet->Refresh();
DataSet->MasterSource = Source;
}
else
{
DataSet->SaveToFile(DataSet->Name);
Result = false;
}
return Result;
}
//---------------------------------------------------------------------------
bool __fastcall TModule::GetConnected()
{
int Date;
return ReceiveDate(&Date);
}
//---------------------------------------------------------------------------
bool __fastcall TModule::ReceiveDate(int *Date)
{
AnsiString Cmd = "What time is it?";
if (0 < IdUDPClient1->Host.Length())
IdUDPClient1->Send(Cmd);
else
IdUDPClient1->Broadcast(Cmd, 422);
return sizeof(Date) == IdUDPClient1->ReceiveBuffer(Date,sizeof(Date),1000);
}
服务器端:
//---------------------------------------------------------------------------
void __fastcall TForm1::IdUDPServer1UDPRead(TObject *Sender,
TStream *AData, TIdSocketHandle *ABinding)
{
//AData->Read(Buf, AData->Size);
//AnsiString BStr = Buf;
TStringStream *BufStrm = new TStringStream(""
;
BufStrm->CopyFrom(AData, AData->Size);
AnsiString BStr = BufStrm->DataString;
delete BufStrm;
if (0 ==BStr.AnsiCompare("where is SQL Server?"
)
{
char *Buf = (char *)IdUDPServer1->LocalName.data();
ABinding->SendTo(ABinding->PeerIP, ABinding->PeerPort, Buf,IdUDPServer1->LocalName.Length());
// 504
Memo1->Lines->Add("Connecting from "+ABinding->PeerIP+":"+ABinding->PeerPort);
}
else
if (0 ==BStr.AnsiCompare("What time is it?"
)
{
int Date = Now();
ABinding->SendTo(ABinding->PeerIP, ABinding->PeerPort, &Date, sizeof(Date));
// 504
Memo1->Lines->Add("Ask time from "+ABinding->PeerIP+":"+ABinding->PeerPort);
}
}