Recent

Author Topic: LNet UDP Listener / Server  (Read 2706 times)

SirTwist

  • New Member
  • *
  • Posts: 21
LNet UDP Listener / Server
« on: February 01, 2020, 11:41:16 am »
Dear programmers,

I need some help with the TLUDP component of the lNet package.

I want to receive UDP datagrams and therefor I set up a TLUdp component and assign a method to the OnReceive handler:
Code: Pascal  [Select][+][-]
  1.   fUDP := TLUdp.Create(self);
  2.   fUDP.OnReceive := @OnDataReceive;
  3.   fUDP.OnError := @OnSocketError;
  4.   fUDP.Listen(21105);
After I started the program, I sent from my linux server some UDP packets to it (via netcat), I see this packets with WireShark, but the OnReceive method is never called.

I went through the LNet documentation, searched for examples, searched this forum and others, but didn't find anything useful.

Has anyone of you some hint or a working example?

Before that I tried to use Indy, but those components have a huge memory leak.

Thank you for your help, kind regards,
Michael

serbod

  • Full Member
  • ***
  • Posts: 142
Re: LNet UDP Listener / Server
« Reply #1 on: March 18, 2020, 03:25:26 pm »
Use .Connect() insead of .Listen().

UDP is not divided to Client or Server, it just connection.

Listen() only for TCP server.
« Last Edit: March 18, 2020, 03:27:48 pm by serbod »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: LNet UDP Listener / Server
« Reply #2 on: July 01, 2022, 07:31:28 pm »
Before that I tried to use Indy, but those components have a huge memory leak.

Indy does not have huge memory leaks. It does have a couple of small leaks, which are intentional and can be disabled if needed.

Use .Connect() insead of .Listen().

UDP is not divided to Client or Server, it just connection.

Listen() only for TCP server.

Listen() works for UDP, too.  Internally it checks the type of socket used.  For UDP, it calls only the platform's socket bind() API.  For TCP, it calls the platform's bind() and listen() APIs.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

BasicOne

  • New Member
  • *
  • Posts: 15
Re: LNet UDP Listener / Server
« Reply #3 on: July 01, 2022, 09:10:14 pm »
listen() is working for me:

Code: Pascal  [Select][+][-]
  1. uses
  2.  ... lNet, lNetComponents, sockets;
  3.  


Code: Pascal  [Select][+][-]
  1.  self.LUDPComponent:=tLUDPComponent.Create(self);
  2.  self.LUDPComponent.Listen(LocalPort,LocalAddress);
  3.  

LocalAddress may be 0.0.0.0 to accept data from any NIC on the PC.

How big are the datagrams you want to receive? Maybe, you have to set the receive buffer to a higher value, e.g. BufSize in bytes
Code: Pascal  [Select][+][-]
  1. function SetRcvBufferSize(BufSize: integer): integer;
  2. var
  3.  optlen: integer;
  4. begin
  5.  optlen := SizeOf(BufSize);
  6.  Result := 0;
  7.  if fpSetSockOpt(self.LUDPComponent.Iterator.Handle, SOL_SOCKET, SO_RCVBUF, @BufSize, optlen) = 0 then
  8.   fpGetSockOpt(self.LUDPComponent.Iterator.Handle, SOL_SOCKET, SO_RCVBUF, @Result, @optlen);
  9. end;
  10.  


and do similar for the send buffer:

Code: Pascal  [Select][+][-]
  1. function SetSndBufferSize(BufSize: integer): integer;
  2. var
  3.  optlen: integer;
  4. begin
  5.  optlen := SizeOf(BufSize);
  6.  Result := 0;
  7.  if fpSetSockOpt(self.LUDPComponent.Iterator.Handle, SOL_SOCKET, SO_SNDBUF, @BufSize, optlen) = 0 then
  8.   fpGetSockOpt(self.LUDPComponent.Iterator.Handle, SOL_SOCKET, SO_SNDBUF, @Result, @optlen);
  9. end;
  10.  

I think, the buffer must be at least as big as the datagram.

 

TinyPortal © 2005-2018