Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7666089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:44:46+00:00 2026-05-31T14:44:46+00:00

I need to create server and client programs with synapse using UDP protocol. I

  • 0

I need to create server and client programs with synapse using UDP protocol.

I have created the server program to listen to any coming messages like this

procedure TForm1.Timer1Timer(Sender: TObject);
var
 resive:string;
begin
  InitSocket;
  resive:=UDPResiveSocket.RecvPacket(1000);
  if resive<>'' then Memo1.Lines.Add('>' + resive);

  DeInitSocket;
end;

procedure TForm1.InitSocket;
begin
  if UDPResiveSocket <> nil then
    DeInitSocket;

  UDPResiveSocket := TUDPBlockSocket.Create;
  UDPResiveSocket.CreateSocket;
  UDPResiveSocket.Bind('0.0.0.0','22401');
  UDPResiveSocket.AddMulticast('234.5.6.7');
  UDPResiveSocket.MulticastTTL := 1;
end;

procedure TForm1.DeInitSocket;
begin
  UDPResiveSocket.CloseSocket;
  UDPResiveSocket.Free;
  UDPResiveSocket := nil;
end;

So i get all incoming messages.
But i want to send a response from the source of this messages.

How can i do that? Does my method is good for server/client?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:44:47+00:00Added an answer on May 31, 2026 at 2:44 pm

    My UDP Echo client / server code. First the server:

    unit UE_Server;
    
    {$mode objfpc}{$H+}
    
    interface
    
    uses
      Classes, SysUtils,
    
      // synapse
      blcksock;
    
    type
    
      { TUEServerThread }
    
      TUEServerThread = class(TThread)
      protected
        procedure Execute; override;
      end;
    
      TUEServer = class
      private
        FUEServerThread: TUEServerThread;
        function GetRunning: Boolean;
      public
        procedure Stop;
        procedure Start;
        property Running: Boolean read GetRunning;
      end;
    
    implementation
    
    { TUEServer }
    
    function TUEServer.GetRunning: Boolean;
    begin
      Result := FUEServerThread <> nil;
    end;
    
    procedure TUEServer.Start;
    begin
      FUEServerThread := TUEServerThread.Create(False);
    end;
    
    procedure TUEServer.Stop;
    begin
      if FUEServerThread <> nil then
      begin
        FUEServerThread.Terminate;
        FUEServerThread.WaitFor;
        FreeAndNil(FUEServerThread);
      end;
    end;
    
    { TUEServerThread }
    
    procedure TUEServerThread.Execute;
    var
      Socket: TUDPBlockSocket;
      Buffer: string;
      Size: Integer;
    begin
      Socket := TUDPBlockSocket.Create;
      try
        Socket.Bind('0.0.0.0', '7');
        try
          if Socket.LastError <> 0 then
          begin
            raise Exception.CreateFmt('Bind failed with error code %d', [Socket.LastError]);
            Exit;
          end;
    
          while not Terminated do
          begin
            // wait one second for new packet
            Buffer := Socket.RecvPacket(1000);
    
            if Socket.LastError = 0 then
            begin
              // just send the same packet back
              Socket.SendString(Buffer);
            end;
    
            // minimal sleep
            if Buffer = '' then
              Sleep(10);
          end;
    
        finally
          Socket.CloseSocket;
        end;
      finally
        Socket.Free;
      end;
    end;
    
    end.
    

    Then the client:

    unit UE_Client;
    
    {$mode objfpc}{$H+}
    
    interface
    
    uses
      {$IFDEF WINDOWS}Windows,{$ENDIF}Classes, SysUtils, DateUtils,
    
      // synapse
      blcksock;
    
    const
      cReceiveTimeout = 2000;
      cBatchSize = 100;
    
    type
      { TUEClient }
    
      TUEClient = class
      private
        FSocket: TUDPBlockSocket;
        FResponseTime: Int64;
      public
        constructor Create;
        destructor Destroy; override;
        procedure Disconnect;
        function Connect(const Address: string): Boolean;
        function SendEcho(const Message: string): string;
        property ReponseTime: Int64 read FResponseTime;
      end;
    
      { TUEAnalyzer }
    
      { TUEAnalyzerThread }
    
      TUEAnalyzerThread = class(TThread)
      private
        FAddress: string;
        FBatchDelay: Cardinal;
        FDropedPackets: Cardinal;
        FAverageResponse: Extended;
        FCriticalSection: TRTLCriticalSection;
        function GetAverageResponse: Extended;
        function GetDropedPackets: Cardinal;
      protected
        procedure Execute; override;
      public
        destructor Destroy; override;
        constructor Create(const Address: string; const BatchDelay: Cardinal);
        property DropedPackets: Cardinal read GetDropedPackets;
        property AverageResponse: Extended read GetAverageResponse;
      end;
    
      TUEAnalyzer = class
      private
        FAddress: string;
        FBatchDelay: Cardinal;
        FAnalyzerThread: TUEAnalyzerThread;
        function GetAverageResponse: Extended;
        function GetDropedPackets: Cardinal;
        function GetRunning: Boolean;
      public
        procedure StopAnalyzer;
        procedure StartAnalyzer;
        property Running: Boolean read GetRunning;
        property Address: string read FAddress write FAddress;
        property DropedPackets: Cardinal read GetDropedPackets;
        property AverageResponse: Extended read GetAverageResponse;
        property BatchDelay: Cardinal read FBatchDelay write FBatchDelay;
      end;
    
    implementation
    
    { TUEAnalyzerThread }
    
    function TUEAnalyzerThread.GetAverageResponse: Extended;
    begin
      EnterCriticalsection(FCriticalSection);
      try
        Result := FAverageResponse;
      finally
        LeaveCriticalsection(FCriticalSection);
      end;
    end;
    
    function TUEAnalyzerThread.GetDropedPackets: Cardinal;
    begin
      EnterCriticalsection(FCriticalSection);
      try
        Result := FDropedPackets;
      finally
        LeaveCriticalsection(FCriticalSection);
      end;
    end;
    
    procedure TUEAnalyzerThread.Execute;
    var
      UEClient: TUEClient;
      Connected: Boolean;
      SendString: string;
      SendCounter: Int64;
      SumResponse: Cardinal;
      SumDropedPackets: Cardinal;
    begin
      UEClient := TUEClient.Create;
      try
        Connected := UEClient.Connect(FAddress);
        try
          if not Connected then
          begin
            raise Exception.CreateFmt('Could not connect UPD client to address %s', [FAddress]);
            Exit;
          end;
    
          SumDropedPackets := 0;
          FAverageResponse := 0;
          FDropedPackets := 0;
          SumResponse := 0;
          SendCounter := 1;
    
          while not Terminated do
          begin
            SendString := IntToStr(SendCounter);
    
            if not (UEClient.SendEcho(SendString) = SendString) then
              Inc(SumDropedPackets);
    
            Inc(SumResponse, UEClient.ReponseTime);
            Inc(SendCounter);
    
            if (SendCounter mod cBatchSize) = 0 then
            begin
              EnterCriticalsection(FCriticalSection);
              try
                FAverageResponse := SumResponse / cBatchSize;
                FDropedPackets := SumDropedPackets;
              finally
                LeaveCriticalsection(FCriticalSection);
              end;
    
              // sleep for specified batch time
              Sleep(FBatchDelay * 1000);
              SumDropedPackets := 0;
              SumResponse := 0;
            end;
    
            // minimal sleep
            Sleep(10);
          end;
        finally
          UEClient.Disconnect;
        end;
      finally
        UEClient.Free;
      end;
    end;
    
    destructor TUEAnalyzerThread.Destroy;
    begin
      {$IFDEF MSWINDOWS}
        DeleteCriticalSection(FCriticalSection)
      {$ELSE}
        DoneCriticalSection(FCriticalSection)
      {$ENDIF};
    
      inherited Destroy;
    end;
    
    constructor TUEAnalyzerThread.Create(const Address: string; const BatchDelay: Cardinal);
    begin
      {$IFDEF MSWINDOWS}
        InitializeCriticalSection(FCriticalSection)
      {$ELSE}
        InitCriticalSection(FCriticalSection)
      {$ENDIF};
    
      FBatchDelay := BatchDelay;
      FreeOnTerminate := True;
      FAddress := Address;
    
      inherited Create(False);
    end;
    
    { TUEAnalyzer }
    
    procedure TUEAnalyzer.StartAnalyzer;
    begin
      FAnalyzerThread := TUEAnalyzerThread.Create(FAddress, FBatchDelay);
    end;
    
    function TUEAnalyzer.GetRunning: Boolean;
    begin
      Result := FAnalyzerThread <> nil;
    end;
    
    function TUEAnalyzer.GetAverageResponse: Extended;
    begin
      Result := FAnalyzerThread.AverageResponse;
    end;
    
    function TUEAnalyzer.GetDropedPackets: Cardinal;
    begin
      Result := FAnalyzerThread.DropedPackets;
    end;
    
    procedure TUEAnalyzer.StopAnalyzer;
    begin
      if Running then
      begin
        FAnalyzerThread.Terminate;
        FAnalyzerThread := nil;
      end;
    end;
    
    { TUEClient }
    
    constructor TUEClient.Create;
    begin
      FSocket := TUDPBlockSocket.Create;
    end;
    
    destructor TUEClient.Destroy;
    begin
      FreeAndNil(FSocket);
    
      inherited Destroy;
    end;
    
    procedure TUEClient.Disconnect;
    begin
      FSocket.CloseSocket;
    end;
    
    function TUEClient.Connect(const Address: string): Boolean;
    begin
      FSocket.Connect(Address, '7');
      Result := FSocket.LastError = 0;
    end;
    
    function TUEClient.SendEcho(const Message: string): string;
    var
      StartTime: TDateTime;
    begin
      Result := '';
      StartTime := Now;
      FSocket.SendString(Message);
    
      if FSocket.LastError = 0 then
      begin
        Result := FSocket.RecvPacket(cReceiveTimeout);
        FResponseTime := MilliSecondsBetween(Now, StartTime);
    
        if FSocket.LastError <> 0 then
        begin
          FResponseTime := -1;
          Result := '';
        end;
      end;
    end;
    
    end.
    

    The code is written in free pascal, but works equally well in Delphi. The client unit is actually a line analyzer that calculates average response times and dropped packets. It is ideal to check the quality of your internet line to a certain server. You put the echo server to the server part and client on the client side.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to create a custom DNS name server using C which will check against
I'm trying to create a client-server program that send and receive a string (its
In sockets I have written the client server program. First I tried to send
I want to create a test program about using socket. First i need to
I am trying to run client server UDP program . My both machines are
I have written a simple client server program in C under linux. I have
I need to write some kind of client-server application using bluetooth. I need to
I need to create a rest server and client. I stumbled upon this tutorial
I have a client program as follows and I need to make it multithreaded
I need to send an object file from my client to the server program.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.