Forum > Networking and Web Programming

LNet UDP Listener / Server

(1/1)

SirTwist:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  fUDP := TLUdp.Create(self);  fUDP.OnReceive := @OnDataReceive;  fUDP.OnError := @OnSocketError;  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:
Use .Connect() insead of .Listen().

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

Listen() only for TCP server.

Remy Lebeau:

--- Quote from: SirTwist on February 01, 2020, 11:41:16 am ---Before that I tried to use Indy, but those components have a huge memory leak.

--- End quote ---

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


--- Quote from: serbod 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.

--- End quote ---

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.

BasicOne:
listen() is working for me:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses ... lNet, lNetComponents, sockets; 


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- self.LUDPComponent:=tLUDPComponent.Create(self); self.LUDPComponent.Listen(LocalPort,LocalAddress); 
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function SetRcvBufferSize(BufSize: integer): integer;var optlen: integer;begin optlen := SizeOf(BufSize); Result := 0; if fpSetSockOpt(self.LUDPComponent.Iterator.Handle, SOL_SOCKET, SO_RCVBUF, @BufSize, optlen) = 0 then  fpGetSockOpt(self.LUDPComponent.Iterator.Handle, SOL_SOCKET, SO_RCVBUF, @Result, @optlen);end; 

and do similar for the send buffer:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function SetSndBufferSize(BufSize: integer): integer;var optlen: integer;begin optlen := SizeOf(BufSize); Result := 0; if fpSetSockOpt(self.LUDPComponent.Iterator.Handle, SOL_SOCKET, SO_SNDBUF, @BufSize, optlen) = 0 then  fpGetSockOpt(self.LUDPComponent.Iterator.Handle, SOL_SOCKET, SO_SNDBUF, @Result, @optlen);end; 
I think, the buffer must be at least as big as the datagram.

Navigation

[0] Message Index

Go to full version