RAD Studio VCL Reference
|
Reads data buffers to the socket.
function ReceiveBuf(var Buf; BufSize: Integer; Flags: Integer = 0): Integer;
__fastcall int ReceiveBuf( Buf, int BufSize, int Flags = 0);
ReceiveBuf reads data buffers of size bufsize to the socket. Use buf to assign a variable to the buffer, and use bufsize to set the size of the buffer to be read. The flags argument can be any of the flags used by the Linux recv function. Refer to recv(2) in the Linux manual pages for more information.
C++ Examples:
/* This example demonstrated the use of TTcpServer in order to receive a buffer (stream) and process it. It is to be used with the example from TTcpClient section. This example uses three buttons, one for activating the server, one for deactivating it and the third for clearing the Memo. */ __fastcall TForm3::TForm3(TComponent* Owner) : TForm(Owner) { Memo1->Lines->Clear(); //server initialization TcpServer1 = new TTcpServer(this); TcpServer1->OnAccept = TcpServer1Accept; TcpServer1->OnCreateHandle = TcpServer1CreateHandle; TcpServer1->OnDestroyHandle = TcpServer1DestroyHandle; } //--------------------------------------------------------------------------- void __fastcall TForm3::TcpServer1Accept(TObject *Sender, TCustomIpClient *ClientSocket) { byte a[10]; ClientSocket->ReceiveBuf(a,10,0); TDateTime now = TDateTime::CurrentDateTime(); Memo1->Lines->Add(DateTimeToStr(now) + " Data added width " + IntToStr(a[9])); for (int i=0; i < 9; i++) { Memo1->Lines->Add(IntToStr(a[i] + a[9])); } Memo1->Lines->Add("--------------------"); } //--------------------------------------------------------------------------- void __fastcall TForm3::Button1Click(TObject *Sender) { TcpServer1->LocalPort = DEFAULT_PORT; TcpServer1->Active = true; } //--------------------------------------------------------------------------- void __fastcall TForm3::Button2Click(TObject *Sender) { TcpServer1->Active = false; } //--------------------------------------------------------------------------- void __fastcall TForm3::Button3Click(TObject *Sender) { Memo1->Lines->Clear(); } //--------------------------------------------------------------------------- void __fastcall TForm3::TcpServer1CreateHandle(TObject *Sender) { TDateTime now = TDateTime::CurrentDateTime(); Memo1->Lines->Add(DateTimeToStr(now) + " Server started."); } //--------------------------------------------------------------------------- void __fastcall TForm3::TcpServer1DestroyHandle(TObject *Sender) { TDateTime now = TDateTime::CurrentDateTime(); if(this->Memo1) { Memo1->Lines->Add(DateTimeToStr(now) + " Server stopped."); } } //---------------------------------------------------------------------------
Delphi Examples:
{ This example demonstrated the use of TTcpServer in order to receive a buffer (stream) and process it. It is to be used with the example from TTcpClient section. } const DEFAULT_PORT = 2501; {$R *.dfm} procedure TForm3.Button1Click(Sender: TObject); begin //connectiong the server TcpServer1.Active := true; end; procedure TForm3.Button2Click(Sender: TObject); begin //disconnecting the server TcpServer1.Active := false; end; procedure TForm3.Button3Click(Sender: TObject); begin //clearing the memo Memo1.Lines.Clear; end; procedure TForm3.FormCreate(Sender: TObject); begin Memo1.Lines.Clear; //server initialization TcpServer1 := TTcpServer.Create(Self); TcpServer1.OnAccept := TCpServer1Accept; TcpServer1.OnCreateHandle := TcpServer1CreateHandle; TcpServer1.OnDestroyHandle := TcpServer1DestroyHandle; TcpServer1.LocalPort := IntToStr(DEFAULT_PORT); end; procedure TForm3.TcpServer1Accept(Sender: TObject; ClientSocket: TCustomIpClient); var a : array[0..9] of byte; I : Cardinal; begin ClientSocket.ReceiveBuf(a,10,0); Memo1.Lines.Add(DateTimeToStr(now) + ' Data added with ' + IntToStr(a[9])); for I := 0 to 8 do begin Memo1.Lines.Add(IntToStr(a[I] + a[9])); end; Memo1.Lines.Add('--------------------'); end; procedure TForm3.TcpServer1CreateHandle(Sender: TObject); begin Memo1.Lines.Add(DateTimeToStr(now) + ' Server started.'); end; procedure TForm3.TcpServer1DestroyHandle(Sender: TObject); begin if Self.Memo1 <> nil then Memo1.Lines.Add(DateTimeToStr(now) + ' Server stopped.'); end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|