Understanding UDP Reconnects and Port Switchover

For UDP "connections," there is also such a thing as reconnects. Due to the very different nature of UDP (compared to TCP), reconnects for UDP must be explained separately. Additionally, we need to introduce something called "port switchover."


Port Switchover Explained

With TCP, each side of the connection uses a single port both to send and receive data. With UDP, this doesn't have to be the case. The sock. object, when it is engaged in a connection, can receive the data from one port, but send the data to a different port!

When port switchover is disabled, the socket always addresses its outgoing UDP datagrams to the port, specified by the sock.targetport property. When port switchover is enabled, the socket will address its outgoing datagrams to the port from which the most recent incoming datagram was received!

Note that we did not say anything about IP switchover — so far we have only discussed ports.


UDP Reconnects

Just like with TCP, the sock.reconmode property defines, for UDP, what kind of incoming UDP datagram will be able to make the socket forget about its previous "connection" and switch to the new one. You have two choices: either define that reconnects are only accepted from a specific IP but any port or choose reconnects to be accepted from any IP and any port. Combine this with two options for port switchover and you have four combinations — four options for the sock.reconmode property. All this is best understood through examples.


Example: PL_SOCK_RECONMODE_0

Setup:

Tibbo BASIC
sock.protocol= PL_SOCK_PROTOCOL_UDP 'we are dealing with UDP
sock.inconmode= PL_SOCK_INCONMODE_ANY_IPPORT 
sock.reconmode= PL_SOCK_RECONMODE_0 'reconnects accepted from the same IP, any port, port switchover off
sock.localportlist= "3000"
sock.targetport= "900"

Two hosts are sending us datagrams and here is how the socket will react:

Incoming datagram

Socket reaction

192.168.100.40:1000 sends a UDP datagram to port 3000 of our device

Datagram accepted, UDP "connection" is now in progress, the socket will be sending all further outgoing datagrams to 192.168.100.40:900.

192.168.100.44:1001 sends a UDP datagram to port 3000 of our device

Datagram ignored — it came from a different IP. The socket will still be sending its outgoing datagrams to 192.168.100.40:900.

192.168.100.40:1100 sends a UDP datagram to port 3000 of our device

Reconnection accepted (so datagram is accepted), but the socket will still be sending all further outgoing datagrams to 192.168.100.40:900 (because port switchover is off).


Example: PL_SOCK_RECONMODE_1

Setup:

Tibbo BASIC
sock.protocol= PL_SOCK_PROTOCOL_UDP 'we are dealing with UDP
sock.inconmode= PL_SOCK_INCONMODE_ANY_IPPORT 
sock.reconmode= PL_SOCK_RECONMODE_1 'reconnects accepted from any IP/port, port switchover off
sock.localportlist= "3000"
sock.targetport= "900"

Two hosts are sending us datagrams and here is how the socket will react:

Incoming datagram

Socket reaction

192.168.100.40:1000 sends a UDP datagram to port 3000 of our device

Datagram accepted, UDP "connection" is now in progress, the socket will be sending all further outgoing datagrams to 192.168.100.40:900.

192.168.100.44:1001 sends a UDP datagram to port 3000 of our device

Reconnection accepted, the socket will be sending all further outgoing datagrams to 192.168.100.44:900.


Example: PL_SOCK_RECONMODE_2

Setup:

Tibbo BASIC
sock.protocol= PL_SOCK_PROTOCOL_UDP 'we are dealing with UDP
sock.inconmode= PL_SOCK_INCONMODE_ANY_IPPORT 
sock.reconmode= PL_SOCK_RECONMODE_2 'reconnects accepted from the same IP, any port, port switchover on
sock.localportlist= "3000"
sock.targetport= "900"

Two hosts are sending us datagrams and here is how the socket will react:

Incoming datagram

Socket reaction

192.168.100.40:1000 sends a UDP datagram to port 3000 of our device

Datagram accepted, UDP "connection" is now in progress, the socket will be sending all further outgoing datagrams to 192.168.100.40:1000.

192.168.100.44:1001 sends a UDP datagram to port 3000 of our device

Datagram ignored — it came from a different IP. The socket will still be sending its outgoing datagrams to 192.168.100.40:1000.

192.168.100.40:1100 sends a UDP datagram to port 3000 of our device

Reconnection accepted, the socket will be sending all further outgoing datagrams to 192.168.100.40:1100.


Example: PL_SOCK_RECONMODE_3

Setup:

Tibbo BASIC
sock.protocol= PL_SOCK_PROTOCOL_UDP 'we are dealing with UDP
sock.inconmode= PL_SOCK_INCONMODE_ANY_IPPORT 
sock.reconmode= PL_SOCK_RECONMODE_3 'reconnects accepted from any IP/port, port switchover on
sock.localportlist= "3000"
sock.targetport= "900"

Now, two hosts are sending us datagrams and here is how the socket will react:

Incoming datagram

Socket reaction

192.168.100.40:1000 sends UDP datagram to the port 3000 of our device

Datagram accepted, UDP "connection" is now in progress, the socket will be sending all further outgoing datagrams to 192.168.100.40:1000.

192.168.100.44:1001 sends UDP datagram to the port 3000 of our device

Reconnection accepted, the socket will be sending all further outgoing datagrams to 192.168.100.44:1001.


Understanding UDP Reconnects and Port Switchover

Port Switchover Explained

UDP Reconnects

Example: PL_SOCK_RECONMODE_0

Example: PL_SOCK_RECONMODE_1

Example: PL_SOCK_RECONMODE_2

Example: PL_SOCK_RECONMODE_3