lNet: Output port of the UDP message lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Giz · 2022-10-26 16:53 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 · 2022-10-26 17:12 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. ---------------------------------------------------------------------- Remy Lebeau · 2022-10-26 18:07 Quote from: Giz on October 26, 2022, 06:53:11 pmTo 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. ---------------------------------------------------------------------- Giz · 2022-10-27 15:16 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...