UDP with pure Ultibo?

Creating, porting and using libraries with Ultibo
pik33
Posts: 879
Joined: Fri Sep 30, 2016 6:30 pm
Location: Poland
Contact:

UDP with pure Ultibo?

Postby pik33 » Thu Mar 28, 2019 6:24 pm

I used Synapse to do this

Code: Select all

procedure TClientThread.execute;

var sock:TUDPBlockSocket;
    buf:string;
    result:boolean;
    IPAddress:string;
    Winsock2TCPClient:TWinsock2TCPClient;

const i:integer=0;

begin
Winsock2TCPClient:=TWinsock2TCPClient.Create;
repeat  IPAddress:=Winsock2TCPClient.LocalAddress; sleep(1000) until ipaddress<>'';
sock:=TUDPBlockSocket.create;
sock.connect('192.168.2.2','12345');
repeat
  i:=i+1;
  buf:='Test string '+inttostr(i);
  sock.sendstring(buf);
  sleep(1000);
until sw2.needclose;
end;       



This is simplest possible thing and it works as I expected, sending test strings to the PC connected to this RPi. Now, can it be done without Synapse, which looks like an overkill for this task?
pjde
Posts: 444
Joined: Tue Feb 09, 2016 6:21 am
Location: Sydney, Australia

Re: UDP with pure Ultibo?

Postby pjde » Thu Mar 28, 2019 8:19 pm

Hi Piotr

This is what I use. Hope it helps.

1) Add a couuple of glable variables

Code: Select all

var
  BroadcastAddr : PSockAddr = nil;
  BroadcastAddrLength : integer = 0;


2) Create a class inherited from TWinsock2UDPListener

Code: Select all

 TMyListener = class (TWinsock2UDPListener)
  protected
    function DoExecute (aThread : TWinsock2UDPServerThread) : Boolean; override;
    procedure DoCreateThread (aServer : TWinsock2UDPServer; var aThread : TWinsock2UDPServerThread);
  public
    constructor Create;
    destructor Destroy; override;
  end; 


where

Code: Select all

function TMyListener.DoExecute (aThread: TWinsock2UDPServerThread): Boolean;
var
  s : string;
begin
  Result := inherited DoExecute (aThread);
  if not Result then exit;
  if aThread.Server.Count > 0 then
    begin
      SetLength (s, aThread.Server.Count);
      Move (aThread.Server.Data^, s[1], aThread.Server.Count);
     Log ('Received ' + s);
    end;
end;

procedure TMyListener.DoCreateThread (aServer: TWinsock2UDPServer;
  var aThread: TWinsock2UDPServerThread);
begin
  aThread := TWinsock2UDPServerThread.Create (aServer);
end;

constructor MyListener.Create;
begin
  inherited Create;
  Threads.Min := 5;
  Threads.Max := 10;
  BoundPort := 12345;
  OnCreateThread := DoCreateThread;
  BroadcastEnabled := true;
  if BroadcastAddr = nil then
    begin
      BroadcastAddrLength := 0;
      BroadcastAddr := AddressToSockAddr ('255.255.255.255', BroadcastAddrLength);
      PortToSockAddr (12346, BroadcastAddr, BroadcastAddrLength);
    end;
end;             

You can then use

Code: Select all

s :='Response Text';
count := 0;
SendToSocket (BroadcastAddr, BroadcastAddrLength, @s[1], length (s), count);       

to transmit.

To start if off

Code: Select all

MyListener := TMyListener.Create;
MyListener.Active := true;

Regards

Paul
User avatar
Ultibo
Site Admin
Posts: 2261
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: UDP with pure Ultibo?

Postby Ultibo » Fri Mar 29, 2019 6:15 am

pik33 wrote:This is simplest possible thing and it works as I expected, sending test strings to the PC connected to this RPi. Now, can it be done without Synapse, which looks like an overkill for this task?

Paul's solution is perfect for a server based purpose where you want to listen for requests and respond to them.

For a purely client end solution that always initiates the request then something like this would be the exact translation of your Synapse example:

Code: Select all

procedure TClientThread.execute;

var
 buf:string;
 IPAddress:string;
 Winsock2UDPClient:TWinsock2UDPClient;
 
const
 i:integer=0;
 
begin
Winsock2UDPClient:=TWinsock2UDPClient.Create;
repeat  IPAddress:=Winsock2UDPClient.LocalAddress; sleep(1000) until ipaddress<>'';

Winsock2UDPClient.RemoteHost := '192.168.2.2';
Winsock2UDPClient.RemotePort := StrToInt('12345');
Winsock2UDPClient.Connect;
repeat
  i:=i+1;
  buf:='Test string '+inttostr(i);
  Winsock2UDPClient.SendData(PChar(buf), Length(buf));
  sleep(1000);
until sw2.needclose;

end;
Ultibo.org | Make something amazing
https://ultibo.org
pik33
Posts: 879
Joined: Fri Sep 30, 2016 6:30 pm
Location: Poland
Contact:

Re: UDP with pure Ultibo?

Postby pik33 » Fri Mar 29, 2019 7:11 am

Thank you, i will test this as soon as I return home - as it is now, all I need is a client sending information to PC so the server side runs on PC and receives those strings
Gavinmc42
Posts: 1630
Joined: Sun Jun 05, 2016 12:38 pm
Location: Brisbane, Australia

Re: UDP with pure Ultibo?

Postby Gavinmc42 » Sat Mar 30, 2019 7:56 am

Found this, Pascal Magazine? Anyway had client/sever ap
https://www.blaisepascalmagazine.eu/pag ... =portal_pt

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 0 guests