Recent

Author Topic: lNet: Output port of the UDP message  (Read 818 times)

Giz

  • Newbie
  • Posts: 2
lNet: Output port of the UDP message
« on: October 26, 2022, 06:53:11 pm »
To send and receive UDP messages (communication with PLC) I need to set (or at least know) the output port of the UDP packet. The controller responds to the same port from which the message came.

How do I set or read the output port of sent UDP message.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: lNet: Output port of the UDP message
« Reply #1 on: October 26, 2022, 07:12:54 pm »
I've never had to do that for UDP, I'm not even sure that it's valid for a connectionless protocol and have generally assumed that the sending port number is the same as the destination. If nothing else, remember that the receiver will be using a function like recvfrom() for UDP and will be explicitly presented with the port and IP address.

MarkMLl

p.s. While I still use LNet on occasion, largely because I've added a few bits to the telnet support of the version that's bundled with FPC, I'm not at all happy about the extent to which it's supported or its reliability. For something as simple as UDP, there's little reason to not use the standard (Berkeley) API with an explicit thread.
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: lNet: Output port of the UDP message
« Reply #2 on: October 26, 2022, 08:07:28 pm »
To send and receive UDP messages (communication with PLC) I need to set (or at least know) the output port of the UDP packet. The controller responds to the same port from which the message came.

How do I set or read the output port of sent UDP message.

You can optionally choose the port you want to use, by explicitly bind()'ing the socket to a specific port number before you sendto() or recvfrom() any packets with that socket.  Or, you can bind() the socket to port 0 to let the OS choose an available ephemeral port for you, and then you can query the socket with getsockname() to find out which port number the OS had chosen.

However, if you are the first party to send anything, you don't really need to explicitly bind() the socket beforehand, just let the OS bind it for you during the 1st sendto() call, and then subsequent recvfrom() calls will receive on that same port.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Giz

  • Newbie
  • Posts: 2
Re: lNet: Output port of the UDP message
« Reply #3 on: October 27, 2022, 05:16:28 pm »
Thanks for the help and ideas. Somehow I managed to solve issue. After sending a UDP packet, receiving is automatically switched to the port from which the packet was sent...

 

TinyPortal © 2005-2018