0

A pc broadcasts every second to other computers on the network. Works fine, except with irregular intervals there is no transmission for 5 seconds. Then it resumes as nothing has happened.

I tried to move the exe file to another pc (to rule out errors on programming pc), but still the same weird result. Here is the screenshot of received data:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

Note data is missing from 24 to 29. It will also be missing later on (not shown here). I have no idea why.

Here is the source code:

unit UClientServer;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, IdUDPServer,
  IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient, IdGlobal, IdSocketHandle;

type
  TForm1 = class(TForm)
    Client: TIdUDPClient;
    Tx: TLabeledEdit;
    BtnStartC: TButton;
    BtnStopC: TButton;
    Timer1: TTimer;
    procedure BtnStartCClick(Sender: TObject);
    procedure BtnStopCClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  TimerTick: word = 0;

procedure TForm1.BtnStartCClick(Sender: TObject);
begin
  Client.Active:= false;
  Client.Host:= '192.168.1.255'; //Broadcast to all computers on this network...
  Client.Port:= 8100;            //...using this port
  Client.Active:= true;
end;

procedure TForm1.BtnStopCClick(Sender: TObject);
begin
  Client.Active:= false;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
//Transmits every second a test message (1,2,3 and so on)
var
  S: string;
begin
  if Client.Active then
  begin
    inc(TimerTick);
    Tx.Text:= IntToStr(TimerTick)+' ';
    S:= S + Tx.Text;
    if TimerTick mod 10 = 0 then S:= S + #13#10;
    Client.Send(S);
  end;
end;

end.

Programming tool: Embarcadero® Delphi 10.2 Tokyo Version 25.0.26309.314

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 4
    That's one of the characteristics of UDP: packets are not guaranteed to arrive in sequence, or even to arrive at all. If you do need this, switch to TCP. – Stijn Sanders Oct 26 '17 at 12:21
  • You should start by understanding what UDP is and how it works. For example: http://www.isoc.org/INET97/proceedings/F3/F3_1.HTM – Jerry Dodge Oct 26 '17 at 12:57
  • 1
    `192.168.1.255` is a subnet broadcast IP (assuming the subnet mask is `255.255.255.0`). You should be using `Client.Broadcast()` instead of `Client.Send()` for subnet broadcasts. The key difference is `Broadcast()` forces the socket's `SO_BROADCAST` option to be enabled, whereas `Send()` does not (you would have to set `Client.BroadcastEnabled := True` before using `Send()`). Either way, use Wireshark or other packet sniffer to monitor UDP traffic on the network. Make sure packets are actually being sent correctly. If they do not arrive on the other end, they are getting lost in transit – Remy Lebeau Oct 26 '17 at 18:46

0 Answers0