Recent

Author Topic: UDP Library/Component Suggestions  (Read 876 times)

lgrfbs

  • Jr. Member
  • **
  • Posts: 61
    • My CV page (On Swedish only)
UDP Library/Component Suggestions
« on: September 21, 2021, 06:51:48 am »
Hello
Now I've found some inspiration again to program a little.
I need a UDP library/component that works for both Linux and Windows, what should I look for?
OS : Win 10 64bit Home * Win 7 64bit Professional
Lazarus 1.8.4 r57972 FPC 3.0.4 i386-win32-win32/win64
Delphi 7.0 (Build 4.453)

sstvmaster

  • Sr. Member
  • ****
  • Posts: 264
Re: UDP Library/Component Suggestions
« Reply #1 on: September 21, 2021, 10:40:25 am »
Here is an overview of network libraries: https://wiki.lazarus.freepascal.org/Networking_libraries

You can use: LNet, Indy or Synapse.
« Last Edit: September 21, 2021, 10:43:26 am by sstvmaster »
Windows 10
lazarus: 2.0.12 / trunk (main)

franciscoluiz

  • New Member
  • *
  • Posts: 25
Re: UDP Library/Component Suggestions
« Reply #2 on: September 21, 2021, 04:51:51 pm »

Warfley

  • Hero Member
  • *****
  • Posts: 554
Re: UDP Library/Component Suggestions
« Reply #3 on: September 23, 2021, 03:00:52 am »
I wouldn't use a library for UDP. UDP is a very simple protocol (as you can only recieve full datagrams every transmission is only a single read or write).

With the sockets unit this is very easy:
Code: Pascal  [Select][+][-]
  1. uses sockets;
  2.  
  3. var
  4.   Sock: TSocket;
  5.   Addr: PSockAddr_in;
  6.   AddrLen: PSizeInt;
  7.   AString: String;
  8.   DataSize: SizeInt;
  9. begin
  10.   Sock := fpsocket(AF_INET, SOCK_DGRAM, 0);
  11.   if Sock < 0 then
  12.     WriteLn('Something  went horribly wrong');
  13.   New(Addr);
  14.   New(AddrLen);
  15.   try
  16.     // Send a string stored in AString to HOST_IP at PORT:
  17.     // Set address of receiver
  18.     Addr^.sin_family := AF_INET;
  19.     Addr^.sin_port := HToNS(PORT);
  20.     Addr^.sin_addr.s_addr := LongWord(StrToNetAddr(HOST_IP));
  21.     // perform sending operation
  22.     fpSendTo(Sock,  Pointer(AString), AString.Length, 0, Addr, SizeOf(Addr^);
  23.     // Receive Data and write to AString:
  24.     SetLength(AString, 512); // 512 is the maximum datagram size in many implementations
  25.     DataSize := fpRecvFrom(Sock, AString, AString.Length, 0, Addr, AddrLen);
  26.     SetLength(AString, DataSize);
  27.     WriteLn('Data received from ', NetAddrToStr(Addr^.sin_addr.s_addr), ': ' . AString);
  28.   finally
  29.     Dispose(Addr);
  30.     Dispose(AddrLen);
  31.   end;
  32. end;

Or to simplify:
Code: Pascal  [Select][+][-]
  1. uses sockets;
  2.  
  3. procedure SendString(Sock: TSocket; const AHost: String; APort: Word, const Data: String);
  4. var
  5.   Addr: TSockAddr_IN;
  6. begin
  7.   Addr^.sin_family := AF_INET;
  8.   Addr^.sin_port := HToNS(APort);
  9.   Addr^.sin_addr.s_addr := LongWord(StrToNetAddr(AHost));
  10.   fpSendTo(Sock, Pointer(Data), Data.Length, 0, @Addr, SizeOf(Addr));
  11. end;
  12.  
  13. procedure ReceiveString(Sock: TSocket; out SenderHost: String; out SenderPort: Word): String;
  14. var
  15.   addr: PSockAddr_IN;
  16.   addrLen: PSizeInt;
  17.   DataSize: SizeInt;
  18. begin
  19.   New(addr);
  20.   New(addrLen);
  21.   try
  22.     SetLength(Result, 512);
  23.     DataSize := fpRecvFrom(Sock, Pointer(Result), Result.Length, 0, addr, addrLen);
  24.     SetLength(Result, DataSize);
  25.     SenderHost := NetAddrToStr(addr^.sin_addr.s_addr);
  26.     SenderPort := NToHs(addr.sin_port);
  27.   finally
  28.     Dispose(addr);
  29.     Dispose(addrLen);
  30.   end;
  31. end;
  32.  
  33. var
  34.   Sock: TSocket;
  35.   data: String;
  36.   HostIP: String = '127.0.0.1';
  37.   HostPort: Word = 1337;
  38. begin
  39.   Sock := fpSocket(AF_INET, SOCK_DGRAM, 0);
  40.   if Sock < 0 then
  41.     Exit; // error;
  42.   // Send console input to host
  43.   ReadLn(data);
  44.   SendString(Sock, HostIP, Port, data);
  45.   // Receive data:
  46.   data := ReceiveString(Sock, HostIP, Port);
  47.   WriteLn('Received from ', HostIP, ':', Port, ' ', data);
  48. end;

This is pretty much all you need to work with UDP. I don't see any reason to include some third party librar for using something that can be expressed with two functions having each less than 10 lines of code
« Last Edit: September 23, 2021, 03:12:32 am by Warfley »

 

TinyPortal © 2005-2018