Sending data from UDPSocket2 to UDPSocket1?

I can’t figure out how to send data from UDPSocket2 to UDPSocket1.

Window1:

[code]Sub Open()

UDPSocket1.Port = 5566
UDPSocket1.Connect // Listen on Port 5566 for data

End Sub
[/code]

UDPSocket1.DataAvailable:

Sub DataAvailable()
  
  Dim MyDatagram As Datagram
  
  MyDatagram = me.Read
  
  Listbox1.AddRow "Data: " + MyDatagram.Data // Display any data received.
  
End Sub
Sub Action()
  
  If UDPSocket2.IsConnected = False Then
    UDPSocket2.Port = 5566
    UDPSocket2.Connect // Connect to send data.
  End If
  
  UDPSocket2.Write "192.168.1.111", "Hello world." // Send data to listening socket UDPSocket2

End Sub

Errors received:

105	AddressInUseError	The address is currently in use.
This error will occur if you attempt to bind to a port that you have already bound to. An example of this would be setting up two listening sockets to try to listen on the same port.

106	InvalidStateError	This is an invalid state error, which means that the socket is not in the proper state to be doing a certain operation.
An example of this is calling the Write method before the socket is actually connected.

I am able to do this in another language, so it seems as though it should be possible.

It would be disappointing to find out that this is a limitation regarding how UDPSockets are implemented in Xojo, if that is the case.

You can’t have 2 sockets listening on the same port. Construct a datagram and set its port to 5566. Connect UDPSocket2 to port 5567 and write the datagram. UDPSocket1 will receive it.

You can send a datagram to any port. You don’t need to be connected to that same port to send it.

UDPSockets don’t work like that. You join a Multicast group on both ends and then you can send the message to the group or to a specific address. Take a look at the example: Communication/Internet/UDPExample.

Now, if you want a computer to specific computer connection, take a look at TCPSockets. On one end you Listen on a port and you Connect from another computer to the first’s TCPIP Address and Port.

You don’t need to be in a multicast group. You can use a udp socket just like a tcpsocket, just without the overhead and certainty. Just tried it in a small project and it worked like a charm.

  dim d as datagram
  
  d= new datagram
  d.port= 5566
  d.data= "Hello World"
  d.Address= "127.0.0.1"
  
  UDPSocket1.port= 5566
  UDPSocket1.connect
  
  UDPSocket2.port= 5567
  UDPSocket2.connect
  UDPSocket2.write d