TsgcWebSocketServer is part of the sgcWebSockets suite and provides a high-level component for creating WebSocket servers in C++Builder. This guide walks through creating a minimal server that automatically replies to incoming messages. 

Create the VCL project

  1. Open C++Builder and create a new VCL Forms Application.
  2. Drop a TsgcWebSocketServer component on the form (named sgcWSServer1 by default).
  3. Optionally add a TMemo (MemoLog) to display activity.

Configure the server

Set up the server in the form's OnCreate handler: 

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  sgcWSServer1->Port   = 9001;  // listen on port 9001
  sgcWSServer1->Active = true;  // start the server
} 

Respond to incoming messages

Handle the OnMessage event to echo the received text back to the client: 

void __fastcall TForm1::sgcWSServer1Message(TsgcWSConnection *Connection,
                                            const UnicodeString Text)
{
  Connection->WriteData("Server received: " + Text);  // reply only to sender
  // sgcWSServer1->Broadcast(Text); // alternative: send to all clients
  MemoLog->Lines->Add(Text);                           // optional logging
}
 

WriteData transmits the response only to the specific connection, whereas Broadcast sends the message to every connected client. 

Run the server

Compile and run the application. Any WebSocket client that connects to ws://<your-host>:9001/ will receive a response prefixed with "Server received:" for every message it sends.