Game Development Reference
In-Depth Information
This looks quite a bit different to how TCP receives packets. With UDP we do not need a
connection and thus we do not need a listener. It is sufficient to have one socket (bound to
a particular port) which can receive packets from any other socket. To see from which cli-
ent (IP address and port) the data is coming from, we have to specify IpAddress and
ushort references, which are filled with the address of the sender and the port. Apart
from that, the method UdpSocket::receive() works in a similar way to its TCP al-
ternative — it requires an array and fills the number of read bytes in the readSize vari-
able. Once we have received the data with no problems, we print the address of the sender
and display the message. In the preceding example , we exit the server (unbinding the
socket) as soon as we have received something from any client.
One UDP socket has the functionality to send or receive messages to or from multiple oth-
er sockets. That makes it possible for a sender to send data to every other listening Ud-
pSocket in a sub-network with a single UdpSocket::send() call. To do that we use
the IpAddress::Broadcast field (255.255.255.255) as an IP address. This sends a
message to every UdpSocket in the sub-network, bound to the 45000 port:
Search WWH ::




Custom Search